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-forum.com/.
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-forum.com/.
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
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 Ternary conditional operator - Wikipedia for more info on ternary op.
hth,
Todd
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 2or:
x == 5 ? puts("one") : puts("two") # Prints two
thanks.
--
Posted via http://www.ruby-forum.com/\.
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?
On Sat, 2008-05-03 at 22:01 +0900, luka luka wrote:
Hi all!
please explain, what is it? what he is doing?
example:x = 6
y = x == 5 ? 0 : 2
puts y // prints 2or:
x == 5 ? puts("one") : puts("two") # Prints two
thanks.
--
Michael T. Richter <ttmrichter@gmail.com> (GoogleTalk:
ttmrichter@gmail.com)
If there's one thing that computers do well, it's to make the same
mistake uncountable times at inhuman speed. (Peter Coffee)
Sebastian Hungerecker wrote:
luka luka wrote:
[Subject: a ? b : c]
please explain, what is it?if a then
b
else
c
endHTH,
Sebastian
Thanks. It's very fine explain (:
--
Posted via http://www.ruby-forum.com/\.
Todd Benson wrote:
x = 6 # assignment
y = x == 5 ? 0 : 2
# if x equals 5, then assign 0 to y
# else assign 2 to yIt's the same as y = (x == 5 ? 0 : 2), because in ruby, == takes
precedence over = (as does the ternary operation)See Ternary conditional operator - Wikipedia for more info on ternary op.
hth,
Todd
Thanks man. I just understand (:
--
Posted via http://www.ruby-forum.com/\.
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-forum.com/\.
Absolutely silly way if all things are constant (just for fun ...
irb(main):001:0> y = [0, 2][x % 5]
Todd
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>
And I have 1 question.
What is: =~
What operator is it?
--
Posted via http://www.ruby-forum.com/.
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
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)
Regular expression matching.
irb(main):001:0> /abc/ =~ "12345abcde"
=> 5
irb(main):002:0>
On Sat, 2008-05-03 at 23:30 +0900, luka luka wrote:
And I have 1 question.
What is: =~
What operator is it?
--
Michael T. Richter <ttmrichter@gmail.com> (GoogleTalk:
ttmrichter@gmail.com)
Those who have learned from history are bound to helplessly watch it
repeat itself. (Albert Y. C. Lai)
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.
On May 3, 10:42 am, Phillip Gawlowski <cmdjackr...@googlemail.com> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1luka 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:
Programming Ruby: The Pragmatic Programmer's Guide
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.comUse 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.orgiEYEARECAAYFAkgceecACgkQbtAgaoJTgL8kZQCgp7JyPWRnN8CIVTiIGcl3HPrb
tSAAoJ8RoWrSNioKwRANqG9ZJ6P4Mc5i
=iYQJ
-----END PGP SIGNATURE-----