How undefined values are handled with 'if' modifier.

  • Thread starter michael.j.cannon
  • Start date
M

michael.j.cannon

Why do these two expressions give different results when foo is undefined?

foo if (foo = 3) # => NameError: undefined local variable or method `foo' for main:Object
if (foo = 3); foo end # => 3

Granted, an operator like ||= does something handy, even though it's inconsistent with foo = foo || 3
but the if-expression-vs-modifier inconsistency just seems wrong.

- Mike
 
R

Robert Klemme

Why do these two expressions give different results when foo is undefined?

foo if (foo = 3) # => NameError: undefined local variable or method `foo' for main:Object
if (foo = 3); foo end # => 3

Granted, an operator like ||= does something handy, even though it's inconsistent with foo = foo || 3
but the if-expression-vs-modifier inconsistency just seems wrong.

That has been discussed numerous times on ruby-talk. That last
discussion is just a few weeks back.

Bottom line: syntactic order in the source determines from when on a
local variable is defined.

Cheers

robert
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top