The behavior of the following code seems like a bug to me:
···
[1, 2, 3, 4].each { |x|
if defined? foo
puts "foo is defined"
else
puts "foo not defined"
foo = true
end
}
[1, 2, 3, 4].each { |x|
if (x % 2) == 0
if defined? bar
puts "bar is defined"
else
puts "bar not defined"
bar = true
end
else # <–
if defined? bar # <–
puts “bar is defined” # <–
else # <–
puts “bar not defined” # <–
bar = true # <–
end # <–
end
}
This produces:
foo not defined
foo not defined
foo not defined
foo not defined
bar is defined
bar not defined
bar is defined
bar not defined
In other words, in the second loop, inside the “highlighted” ‘else’ clause
– and only inside that clause – ‘defined? bar’ evaluates to
"local-variable(in-block)" for some reason. This is either a bug or
something so subtle it’s beyond me…
I didn’t find anything in ruby-talk archives about it, and the behavior is
consistent among the following versions:
ruby 1.6.5 (2001-09-19) [i686-linux]
ruby 1.6.7 (2002-06-04) [i686-linux]
ruby 1.7.2 (2002-06-04) [i686-linux]
Cheers,
- jeff