Friday 16 September 2011

Scope of block variables in Ruby 1.8 and 1.9


There is a difference in the scope of the variables in blocks w.e.f ruby 1.8 and 1.9  I have pasted en example to high light the change. It created trouble when I moved my application from 1.8 to 1.9


a = 1
b = 2
r = 0
n =0

(0..10).each do |n|
  a = n
  r = r  + n
end

puts "a = #{a} ; b = #{b} ; r = #{r} ; n = #{n}"


ruby 1.8.7
  a = 10 ; b = 2 ; r = 55 ; n = 10

ruby 1.9.2
  a = 10 ; b = 2 ; r = 55 ; n = 0





No comments:

Post a Comment