I have run across a bug in Enumerable#map. It affects 1.5-dev but is
okay in 1.4.0.
Unfortunately, I can’t figure out how to reproduce it to a simple
example. I’ll show in pseudo-code what I’m doing and the incorrect
behavior I am seeing. I hope that is enough to find the problem.
loop do
ladders = @strategies.map { |strategy| strategy.crank(bar) }
if verbose && ladders.any? { |ladder| ladder.changed? }
ladder = Ladder.consolidate ladders
write ladder
end
# unrelated work
end
This code calls #crank on my strategy class and returns a Ladder object.
Obviously, it should assign a new array to the +ladders+ variable each
time. However, I see a situation where on one pass through the loop
+ladders+ gets 2 Ladder objects assigned. The next time through one
strategy goes away so #crank is called once and returns a single
Ladder object. This time the +ladders+ variable is assigned [Ladder,
nil] for some reason. This causes my code to crash because I don’t check
for nil (it should never return a nil).
I tried to “fix” this by calling #compact after the map operation but
that crashes jruby with an exception [1] which proves to me it isn’t my
code.
I also tried to shine some light on this by creating another local +i+
and incrementing it inside the map block, but when I run it that way the
block is never called. It’s all very weird.
map block never gets called!
i = 0
ladders = @strategies.map { |strategy|
i += 1
strategy.crank(bar)
}
puts "ladders.size [#{ladders.size}] i [#{i}]" # always prints
‘ladders.size [0] i [0]’
if verbose && ladders.any? { |ladder| ladder.changed? }
ladder = Ladder.consolidate ladders
write ladder
end
Any ideas?
cr
[1] http://gist.github.com/302710
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email