From: 7stud -- Date: 2008-01-29T12:48:30+09:00 Subject: 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 =<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 # (NameError) -- Posted via http://www.ruby-forum.com/.