What's difference?

Discussion in 'Ruby' started by Kyung won Cheon, Aug 18, 2008.

  1. --- test1.rb ---

    b = 1
    puts a if a = b

    # NameError: undefined local variable or method `a' for main:Object


    --- test2.rb ---

    a = 1 if false
    puts defined?(a)

    # local-variable


    ###############
    # Help Me^^
    ###############
    --
    Posted via http://www.ruby-forum.com/.
     
    Kyung won Cheon, Aug 18, 2008
    #1
    1. Advertising

  2. On Aug 18, 2008, at 8:29, Kyung won Cheon wrote:

    > --- test1.rb ---
    >
    > b =3D 1
    > puts a if a =3D b
    >
    > # NameError: undefined local variable or method `a' for main:Object
    >



    Parser peculiarity. You can't use a local variable defined for the =20
    first time in the clause of an inline-if for that statement. Define a =20=

    before it reaches that line, or use this instead:

    if a =3D b then puts a end

    --=20
    # Mikael H=F8ilund
    def method_missing(m, a=3D0) a +
    m.to_s[/[a-z]+/].size * 2; end
    p What is the meaning of life?
     
    Mikael Høilund, Aug 18, 2008
    #2
    1. Advertising

  3. Kyung won Cheon

    psy Guest

    Hier from Programming Ruby book. Its not exactly for this case, but i think it gets close :

    ----
    When Ruby sees a name such as ``a'' in an expression, it needs to determine if it is a local variable reference or a call to a method with no parameters. To decide which is the case, Ruby uses a
    heuristic. As Ruby reads a source file, it keeps track of symbols that have been assigned to. It assumes that these symbols are variables. When it subsequently comes across a symbol that might be
    either a variable or a method call, it checks to see if it has seen a prior assignment to that symbol. If so, it treats the symbol as a variable; otherwise it treats it as a method call.
    ----


    There is also this example down below :

    ----
    Note that the assignment does not have to be executed---Ruby just has to have seen it. This program does not raise an error.

    a = 1 if false; a
    ----

    Greets

    On Mon, 18 Aug 2008 15:29:04 +0900
    Kyung won Cheon <> wrote:

    > --- test1.rb ---
    >
    > b = 1
    > puts a if a = b
    >
    > # NameError: undefined local variable or method `a' for main:Object
    >
    >
    > --- test2.rb ---
    >
    > a = 1 if false
    > puts defined?(a)
    >
    > # local-variable
    >
    >
    > ###############
    > # Help Me^^
    > ###############
     
    psy, Aug 18, 2008
    #3
    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. jakk
    Replies:
    4
    Views:
    12,610
  2. Nick Timkovich
    Replies:
    0
    Views:
    84
    Nick Timkovich
    Feb 25, 2014
  3. Skip Montanaro
    Replies:
    0
    Views:
    83
    Skip Montanaro
    Feb 25, 2014
  4. Peter Otten
    Replies:
    1
    Views:
    102
    Mark Lawrence
    Feb 25, 2014
  5. Peter Otten
    Replies:
    0
    Views:
    74
    Peter Otten
    Feb 25, 2014
Loading...

Share This Page