A ? b : c

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.

luka luka wrote:

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

if a then
b
else
c
end

HTH,
Sebastian

On Sat, May 3, 2008 at 8:01 AM, luka luka [email protected] wrote:

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

thanks.

Posted via http://www.ruby-forum.com/.

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.org/wiki/%3F: for more info on ternary op.

hth,
Todd

Sebastian H. 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 (:

Todd B. 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.org/wiki/%3F: for more info on ternary op.

hth,
Todd

Thanks man. I just understand (:

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.

On Sat, May 3, 2008 at 8:25 AM, Michael T. Richter
[email protected] wrote:

irb(main):008:1> end
=> 2
irb(main):009:0>

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

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

Todd

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

On Sat, 2008-05-03 at 22:01 +0900, luka luka wrote:

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

thanks.

    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?

-----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/ProgrammingRuby/

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

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 G.
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

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.mozdev.org

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

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

On Sat, 2008-05-03 at 23:30 +0900, luka luka wrote:

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

Regular expression matching.

    irb(main):001:0> /abc/ =~ "12345abcde"
    => 5
    irb(main):002:0>

On May 3, 10:42 am, Phillip G. [email protected]
wrote:

The Ruby Way, too, if you can).

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 P.ming 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs