Anibal
(Aníbal)
20 March 2010 12:40
1
Hello,
I am kind of lost with the nil result while I am expecting a false:
irb(main):001:0> x = 'foo' if false
=> nil
irb(main):002:0> x
=> nil
irb(main):003:0> x = 'foo' if true
=> "foo"
irb(main):004:0> x
=> "foo"
Thanks in advance for sharing your knowledge,
···
--
Anìbal Rojas
Chris6
(Chris)
20 March 2010 12:40
2
See Programming Ruby: The Pragmatic Programmer's Guide
'if' doesn't return the logical value it evaluates, it returns the
value of the last statement
executed, in the false case nothing is executed, and nil is Ruby's
nothing value
Cheers
···
On Mar 17, 12:01 pm, Aníbal Rojas <anibalro...@gmail.com> wrote:
Hello,
I am kind of lost with the nil result while I am expecting a false:
irb(main):001:0> x = 'foo' if false
=> nil
irb(main):002:0> x
=> nil
irb(main):003:0> x = 'foo' if true
=> "foo"
irb(main):004:0> x
=> "foo"
Thanks in advance for sharing your knowledge,
--
Anìbal Rojas
Another way to think about it is that the above constructs are
alternate ways of expressing the following:
if false then x = 'foo' end
if true then x = 'foo' end
-- Mark.
···
On Mar 17, 12:01 pm, Aníbal Rojas <anibalro...@gmail.com> wrote:
Hello,
I am kind of lost with the nil result while I am expecting a false:
irb(main):001:0> x = 'foo' if false
=> nil
irb(main):002:0> x
=> nil
irb(main):003:0> x = 'foo' if true
=> "foo"
irb(main):004:0> x
=> "foo"
Thanks in advance for sharing your knowledge,
Anibal
(Aníbal)
20 March 2010 12:41
4
Thanks for your responses!
···
--
Aníbal
On Mar 18, 2:44 pm, Mark Thomas <m...@thomaszone.com> wrote:
On Mar 17, 12:01 pm, Aníbal Rojas <anibalro...@gmail.com> wrote:
> Hello,
> I am kind of lost with thenilresult while I am expecting a false:
> irb(main):001:0> x = 'foo'iffalse
> =>nil
> irb(main):002:0> x
> =>nil
> irb(main):003:0> x = 'foo'iftrue
> => "foo"
> irb(main):004:0> x
> => "foo"
> Thanks in advance for sharing your knowledge,
Another way to think about it is that the above constructs are
alternate ways of expressing the following:
iffalse then x = 'foo' endiftrue then x = 'foo' end
-- Mark.