On Tue, Feb 28, 2006 at 02:05:17AM +0900, 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
> 
[...]
> This problem is NOT there if the code is on one line:
> 
> puts Class.new {def to_s ; 'no bug'; end }.new
>
> 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
  ==============
  I can't reproduce this with 1.8.4, are you sure?
> results are the same.

The original code works as expected (do... end binds to puts, not Class.new),
and I can't see anything strange relative to multi- vs. single-line; {} and
do/end seem to work consistently as advertised:

RUBY_VERSION                                       # => "1.8.4"

# "no bug" expected
puts Class.new {def to_s ; 'no bug'; end }.new
# >> no bug

# ditto 
puts Class.new {
  def to_s; 'no bug' end
}.new
# >> no bug

# expect Class#inspect on stdout and undefined 'new' for nil (puts' return
# value)

puts Class.new do def to_s ; 'no bug'; end end.new
# ~> -:1: undefined method `new' for nil:NilClass (NoMethodError)
# >> #<Class:0xb7dd0f70>


puts Class.new do 
  def to_s ; 'no bug'; end 
end.new
# ~> -:3: undefined method `new' for nil:NilClass (NoMethodError)
# >> #<Class:0xb7d5af48>

-- 
Mauricio Fernandez  -   http://eigenclass.org   -  non-trivial Ruby