[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

a ? b : c

luka luka

5/3/2008 1:01:00 PM

Hi all!
please explain, what is it? what he is doing?
example:

x = 6
y = x == 5 ? 0 : 2
puts y // prints 2

or:

x == 5 ? puts("one") : puts("two") # Prints two

thanks.
--
Posted via http://www.ruby-....

10 Answers

Sebastian Hungerecker

5/3/2008 1:13:00 PM

0

luka luka wrote:
> [Subject: a ? b : c]
> please explain, what is it?

if a then
b
else
c
end


HTH,
Sebastian
--
NP: Depeche Mode - Behind The Wheel
Jabber: sepp2k@jabber.org
ICQ: 205544826

Todd Benson

5/3/2008 1:16:00 PM

0

On Sat, May 3, 2008 at 8:01 AM, luka luka <dezertir@posta.ge> wrote:
> Hi all!
> please explain, what is it? what he is doing?
> example:
>
> x = 6
> y = x == 5 ? 0 : 2
> puts y // prints 2
>
> or:
>
> x == 5 ? puts("one") : puts("two") # Prints two
>
> thanks.
> --
> Posted via http://www.ruby-....

x = 6 # assignment
y = x == 5 ? 0 : 2
# if x equals 5, then assign 0 to y
# else assign 2 to y

It's the same as y = (x == 5 ? 0 : 2), because in ruby, == takes
precedence over = (as does the ternary operation)

See http://en.wikipedia.or...: for more info on ternary op.

hth,
Todd

luka luka

5/3/2008 1:18:00 PM

0

Sebastian Hungerecker wrote:
> luka luka wrote:
>> [Subject: a ? b : c]
>> please explain, what is it?
>
> if a then
> b
> else
> c
> end
>
>
> HTH,
> Sebastian

Thanks. It's very fine explain (:
--
Posted via http://www.ruby-....

luka luka

5/3/2008 1:21:00 PM

0

Todd Benson wrote:
> x = 6 # assignment
> y = x == 5 ? 0 : 2
> # if x equals 5, then assign 0 to y
> # else assign 2 to y
>
> It's the same as y = (x == 5 ? 0 : 2), because in ruby, == takes
> precedence over = (as does the ternary operation)
>
> See http://en.wikipedia.or...: for more info on ternary op.
>
> hth,
> Todd

Thanks man. I just understand (:
--
Posted via http://www.ruby-....

luka luka

5/3/2008 1:33:00 PM

0

Michael T. Richter wrote:
> irb(main):001:0> x = 6
> => 6
> irb(main):002:0> y = x == 5 ? 0 : 2
> => 2
> irb(main):003:0> x = 6
> => 6
> irb(main):004:0> y = if x == 5
> irb(main):005:1> 0
> irb(main):006:1> else
> irb(main):007:1* 2
> irb(main):008:1> end
> => 2
> irb(main):009:0>
>
> Get it?

Yes thanks. I get it.
--
Posted via http://www.ruby-....

Todd Benson

5/3/2008 1:35:00 PM

0

On Sat, May 3, 2008 at 8:25 AM, Michael T. Richter <ttmrichter@gmail.com> wrote:

> irb(main):001:0> x = 6
> => 6
> irb(main):002:0> y = x == 5 ? 0 : 2
> => 2
> irb(main):003:0> x = 6
> => 6
> irb(main):004:0> y = if x == 5
> irb(main):005:1> 0
> irb(main):006:1> else
> irb(main):007:1* 2
> irb(main):008:1> end
> => 2
> irb(main):009:0>

Absolutely silly way if all things are constant (just for fun :) ...

irb(main):001:0> y = [0, 2][x % 5]

Todd

luka luka

5/3/2008 2:31:00 PM

0

And I have 1 question.
What is: =~
What operator is it?
--
Posted via http://www.ruby-....

Sebastian Hungerecker

5/3/2008 2:36:00 PM

0

luka luka wrote:
> What is: =~
> What operator is it?

For strings and regexen it is defined to return the position at which the
regex matches string. It is not defined for any other classes in core ruby
as far as I am aware.

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Phillip Gawlowski

5/3/2008 2:43:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

luka luka wrote:
| And I have 1 question.
| What is: =~
| What operator is it?

I strongly suggest you avail yourself of a copy of Programming Ruby (and
The Ruby Way, too, if you can).

The first edition of Programming Ruby is available free of charge online:

http://www.ruby-doc.org/docs/Progra...

This will get you started on the basics of Ruby, and saves you the
annoyance of checking your inbox every two minutes. :)


N.B.: Programming Ruby 1st Edition covers Ruby 1.6, but the basics
haven't really changed (i.e. operators, keywords, and the like).

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.bl...

Use the good features of a language; avoid the bad ones.
~ - The Elements of Programming Style (Kernighan & Plaugher)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkgceecACgkQbtAgaoJTgL8kZQCgp7JyPWRnN8CIVTiIGcl3HPrb
tSAAoJ8RoWrSNioKwRANqG9ZJ6P4Mc5i
=iYQJ
-----END PGP SIGNATURE-----

Wyatt Greene

5/4/2008 2:00:00 AM

0

On May 3, 10:42 am, Phillip Gawlowski <cmdjackr...@googlemail.com>
wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> luka luka wrote:
>
> | And I have 1 question.
> | What is: =~
> | What operator is it?
>
> I strongly suggest you avail yourself of a copy of Programming Ruby (and
> The Ruby Way, too, if you can).
>
> The first edition of Programming Ruby is available free of charge online:
>
> http://www.ruby-doc.org/docs/Progra...
>
> This will get you started on the basics of Ruby, and saves you the
> annoyance of checking your inbox every two minutes. :)
>
> N.B.: Programming Ruby 1st Edition covers Ruby 1.6, but the basics
> haven't really changed (i.e. operators, keywords, and the like).
>
> - --
> Phillip Gawlowski
> Twitter: twitter.com/cynicalryan
> Blog:http://justarubyist.bl...
>
> Use the good features of a language; avoid the bad ones.
> ~ - The Elements of Programming Style (Kernighan & Plaugher)
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.8 (MingW32)
> Comment: Using GnuPG with Mozilla -http://enigmail....
>
> iEYEARECAAYFAkgceecACgkQbtAgaoJTgL8kZQCgp7JyPWRnN8CIVTiIGcl3HPrb
> tSAAoJ8RoWrSNioKwRANqG9ZJ6P4Mc5i
> =iYQJ
> -----END PGP SIGNATURE-----

For what it's worth:

The ?: operator is called the "conditional operator". It is the only
ternary operator in Ruby (meaning the only operator that has three
operands).

The =~ is called the "pattern-matching operator". It is related to
the !~ operator, which returns the boolean opposite of the =~
operator.

If you want a good, solid reference book in Ruby, I would recommend
"The Ruby Programming Language" by David Flanagan and Yukihiro
Matsumoto. It might not be the best first book on Ruby, but in my
opinion it is the most complete reference book available about the
Ruby language.