How undefined values are handled with 'if' modifier.

Discussion in 'Ruby' started by michael.j.cannon, Dec 7, 2012.

  1. 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
     
    michael.j.cannon, Dec 7, 2012
    #1
    1. Advertisements

  2. 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
     
    Robert Klemme, Dec 8, 2012
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.