what's the diff between puts y and puts "#{y}" in class_eval

Discussion in 'Ruby' started by Raj Singh, Jan 28, 2008.

  1. Raj Singh

    Raj Singh Guest

    Look at the code.

    y = " another hi"

    class Wes
    end

    Wes.class_eval <<-END
    def hello
    puts "#{y}"
    puts y # I get wrong number of arguments error
    end
    END


    While puts "#{y}" works puts y fails. And I am not sure what's the
    explanation.

    Any thoughts.
    --
    Posted via http://www.ruby-forum.com/.
    Raj Singh, Jan 28, 2008
    #1
    1. Advertising

  2. Raj Singh

    Raj Singh Guest

    Re: what's the diff between puts y and puts "#{y}" in class_

    Hi Matz,

    Thanks for the answer but I couldn't quite follow it.

    It seems that you are suggesting that the scope of "#{y}" is different
    from the scope of plain vanilla 'y' in "puts y".

    Could you please further elaborate on that. To me it seems like both of
    them have the same scope because they are both being used in the same
    method.
    --
    Posted via http://www.ruby-forum.com/.
    Raj Singh, Jan 28, 2008
    #2
    1. Advertising

  3. Raj Singh

    Ryan Ingram Guest

    Re: what's the diff between puts y and puts "#{y}" in class_

    On 1/28/08, Raj Singh <> wrote:
    > Could you please further elaborate on that. To me it seems like both of
    > them have the same scope because they are both being used in the same
    > method.


    Compare to this:

    class T
    def first
    "hello"
    end
    def second
    "chunky bacon"
    end
    end

    y = "first"

    T.class_eval <<-END
    def hello
    puts #{y}
    end
    END

    T.new.hello # prints "hello"

    The key is that the "<<-END" is creating an interpolated string in the
    original context; the interpolation of #{y} isn't happening during the
    call to "hello", but rather as part of the string passed to
    class_eval.

    You are probably confused by the fact that in the sample code, the
    interpolation is happening inside of another string constant, which
    makes it look like it's happening as part of "hello". It's not.

    -- ryan
    Ryan Ingram, Jan 29, 2008
    #3
  4. Raj Singh

    7stud -- Guest

    Re: what's the diff between puts y and puts "#{y}" in class_

    Ryan Ingram wrote:
    > On 1/28/08, Raj Singh <> wrote:
    >> Could you please further elaborate on that. To me it seems like both of
    >> them have the same scope because they are both being used in the same
    >> method.

    >
    > Compare to this:
    >
    > class T
    > def first
    > "hello"
    > end
    > def second
    > "chunky bacon"
    > end
    > end
    >
    > y = "first"
    >
    > T.class_eval <<-END
    > def hello
    > puts #{y}
    > end
    > END
    >
    > T.new.hello # prints "hello"
    >
    > The key is that the "<<-END" is creating an interpolated string in the
    > original context; the interpolation of #{y} isn't happening during the
    > call to "hello", but rather as part of the string passed to
    > class_eval.
    >
    > You are probably confused by the fact that in the sample code, the
    > interpolation is happening inside of another string constant, which
    > makes it look like it's happening as part of "hello". It's not.
    >
    > -- ryan


    Thanks for the explanation, I've been watching this thread and waiting
    for an explanation that I could comprehend. If I understand your
    explanation correctly, the steps are:

    1) The substitution in the string:

    str =<<STR
    def hello
    puts #{y}
    end
    STR

    is performed. The scope is the global scope, so the string becomes:

    str =<<STR
    def hello
    puts first
    end
    STR

    2) The string is class_eval'ed in the scope of class T, producing:

    class T
    def first
    "hello"
    end

    def second
    "chunky bacon"
    end

    def hello
    puts first
    end
    end

    3) When the method hello is called, the line:

    puts first

    outputs the return value of the first method.


    Applying those steps to the op's example:

    >y = " another hi"
    >
    >class Wes
    >end
    >
    >Wes.class_eval <<-END
    >def hello
    > puts "#{y}"
    > puts y # I get wrong number of arguments error
    > end
    >END


    1) The string:

    ><<-END
    >def hello
    > puts "#{y}"
    > puts y # I get wrong number of arguments error
    >end
    >END


    becomes:

    <<-END
    def hello
    puts "another hi"
    puts y # I get wrong number of arguments error
    end
    END

    2) The class Wes is transformed into:

    class Wes
    def hello
    puts "another hi"
    puts y
    end
    end

    3) Then a statement such as:

    w = Wes.new
    w.hello

    produces an error because there is no variable or method named 'y'
    inside class Wes:


    rb:4:in `hello': undefined local variable or method `y' for
    #<Wes:0x25328> (NameError)




    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Jan 29, 2008
    #4
  5. Raj Singh

    Ryan Ingram Guest

    Re: what's the diff between puts y and puts "#{y}" in class_

    Yep, you've got it exactly.

    On 1/28/08, 7stud -- <> wrote:
    > Thanks for the explanation, I've been watching this thread and waiting
    > for an explanation that I could comprehend. If I understand your
    > explanation correctly, the steps are:
    >
    > 1) The substitution in the string:
    >
    > str =<<STR
    > def hello
    > puts #{y}
    > end
    > STR
    >
    > is performed. The scope is the global scope, so the string becomes:
    >
    > str =<<STR
    > def hello
    > puts first
    > end
    > STR
    >
    > 2) The string is class_eval'ed in the scope of class T, producing:
    >
    > class T
    > def first
    > "hello"
    > end
    >
    > def second
    > "chunky bacon"
    > end
    >
    > def hello
    > puts first
    > end
    > end
    >
    > 3) When the method hello is called, the line:
    >
    > puts first
    >
    > outputs the return value of the first method.
    Ryan Ingram, Jan 29, 2008
    #5
    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. Cyril Vi?ville

    diff Process under diff users

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    1
    Views:
    509
    Joe Smith
    Jun 29, 2004
  2. Berrucho
    Replies:
    2
    Views:
    635
    Infant Newbie
    Dec 5, 2003
  3. A Traveler

    Diff CSS styles for diff INPUT TYPE='s?

    A Traveler, Aug 31, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,923
    Steve Fulton
    Aug 31, 2004
  4. Santa
    Replies:
    1
    Views:
    1,075
    Mark A. Odell
    Jul 17, 2003
  5. Michael Brooks
    Replies:
    22
    Views:
    409
    Rick DeNatale
    Mar 27, 2007
Loading...

Share This Page