How undefined values are handled with 'if' modifier.

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

  1. Guest

    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
    , Dec 7, 2012
    #1
    1. Advertising

  2. On 12/07/2012 07:02 PM, wrote:
    > 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
    Robert Klemme, Dec 8, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter
    Replies:
    0
    Views:
    16,915
    Peter
    Jul 11, 2003
  2. Ajith Nair
    Replies:
    3
    Views:
    6,888
    Ajith Nair
    Aug 25, 2005
  3. Sherif ElMetainy

    EndRequest Event is not handled

    Sherif ElMetainy, Aug 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    764
    Sherif ElMetainy
    Aug 30, 2003
  4. sanjay patil via .NET 247

    Previous(Unwanted) event handled when page refreshed by key F5

    sanjay patil via .NET 247, Apr 27, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    388
    sanjay patil via .NET 247
    Apr 27, 2004
  5. Kostia
    Replies:
    0
    Views:
    348
    Kostia
    Apr 27, 2004
Loading...

Share This Page