a ? b : c

L

luka luka

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.
 
T

Todd Benson

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.

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/?: for more info on ternary op.

hth,
Todd
 
L

luka luka

Todd said:
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/?: for more info on ternary op.

hth,
Todd

Thanks man. I just understand :)
 
L

luka luka

Michael said:
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.
 
T

Todd Benson

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
 
S

Sebastian Hungerecker

luka said:
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
 
P

Phillip Gawlowski

-----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. :)


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.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-----
 
W

Wyatt Greene

-----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. :)

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,066
Members
47,673
Latest member
MahaliaPal

Latest Threads

Top