From: Loqi Tamaroon <redmine@...>
Date: 2009-01-09T10:30:54+09:00
Subject: [ruby-core:21226] [Bug #992] Hash becomes Array for no apparent reason.

Bug #992: Hash becomes Array for no apparent reason.
http://redmine.ruby-lang.org/issues/show/992

Author: Loqi Tamaroon
Status: Open, Priority: Normal
Target version: Ruby 1.8.6

# Using Ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
#   on Mac OS X 10.5.5
def is_this_a_ruby_bug?
  orig_hash = { :a=>[:x], :b=>[:x] }
  orig_hash.inject({}) do | flipped_hash, (symbol_a_or_b, array_x) |
    array_x.each do |symbol_x|
      (flipped_hash[symbol_x] ||= []) << symbol_a_or_b   # (~1)
      flipped_hash                                       # (~2)
    end                                                  # (~3)
#   flipped_hash                                         # (~4)
  end
end
is_this_a_ruby_bug?
# The above method is supposed to return { :x => [:a, :b] } . Instead, it exhibits
# bizarre behavior. On the first iteration of the inner loop, line (~2) somehow causes
# flipped_hash to be recast from Hash {:x=>[:a]} to Array [:x] . This causes (~1) to
# raise a "Symbol as array index (TypeError)" exception on the second iteration of
# array_x.each , because flipped_hash has accedentally become an Array. A workaround
# is to uncomment (~4), which curiously is OUTSIDE the inner loop where the exception
# would've occured. The effect is that the 'end' at (~3) no longer terminates the inner
# loop and (~4) is now executed with each iteration of array_x.each as if it were above
# (~3). Somehow this fixes the problem, and the method returns the expected result.


----------------------------------------
http://redmine.ruby-lang.org