Search code examples
rubyexceptionruby-1.9ruby-1.9.2ruby-1.8

Why begin/rescue/else behaves differently on 1.9.2 and 1.8.7


I am working with the method mm. In ruby 1.9.2 it behaves weird, instead of the expected result 1.9.2=>10 I am getting

ELSE **
1.9.2=>8

Any idea of what is going on?

class A

 def mm(data)
   begin
     send_len = data
     return send_len
   rescue Exception
     STDOUT.write("Rescue *#{$!}*\n")
   else
     STDOUT.write("ELSE *#{$!}*\n")
   end
 end

end # class A

a = A.new
print "#{RUBY_VERSION}=>#{a.mm(10)}\n"

With 1.8.7 I get the expected result:

1.8.7=>10

Solution

  • It's an open bug in Ruby. There is a discussion, though, whether it should behave like it behaved in 1.8 or as it does in 1.9.

    Matz, the author of Ruby, believes that it should behave as in 1.8.