Walk a Hash with SyncEnumerator

February 15th, 2007

I again was fumbling through PickAxe version 2.0 and stumbled onto SyncEnumerator. One use I see for SyncEnumerator is to walk both the keys and values of a hash, like:

require ‘generator’

ahash = {:five => 5, :six => 6, :seven => 7}
SyncEnumerator.new(ahash.keys, ahash.values).each do |k,v|
  puts "#{k} equals #{v}"
end

The pickaxe is quickly becoming very valuable to me.

Leave a Reply