Question about the evaluation of x = 'foo' if false

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,

On Mar 17, 12:01 pm, Aníbal Rojas [email protected] wrote:

irb(main):004:0> x
=> “foo”

Thanks in advance for sharing your knowledge,


Anìbal Rojas

See
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UH
‘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 [email protected] wrote:

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:

if false then x = ‘foo’ end
if true then x = ‘foo’ end

– Mark.

Thanks for your responses!


Aníbal

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