On Monday 27 February 2006 18:05, David Smiley wrote:
> I have encountered a counter-intuitive bug in Ruby's parser.  The
> following sample shows the problem:
>
> puts Class.new do
>    def to_s
>       'no bug'
>    end
> end.new
>
> What do you think the output should be?  I think it should clearly
> be 'no bug' but instead it is '#<Class:0x73be8>' and then an error
> complaining that there is no method 'new' on 'NilClass'.  See, Ruby
> invokes the call to 'new' AFTER the call to puts.  To work around
> this problem, use of parenthesis are necessary.
>
> This problem is NOT there if the code is on one line:
>
> puts Class.new {def to_s ; 'no bug'; end }.new

It's not the difference between one and multiple lines.
Braces and do/end have different precedence. Replace the do/end
in the first example with braces and you get your expected
behaviour. The Rake (and Rant) DSL utilizes the above do/end 
behaviour. Eventually read the end of
http://rake.rubyforge.org/files/doc/rakefile_rdoc.html

> The fact that this shows up for the multiple line but not the
> single line sample should be a sign that there is indeed a problem.
>  And it doesn't matter whether the brackets or do-end notation is
> used; the results are the same.
>
> I encountered this problem when I was using Rails to set the
> around_filter of a controller by using an anonymous class.
>
> I tested this against Ruby 1.8.4.
>
> ~ David Smiley
>    MITRE

Regards,
 Stefan