From: ara.t.howard@... Date: 2006-08-16T01:17:55+09:00 Subject: Re: Hash#each On Wed, 16 Aug 2006, Trans wrote: > > phrogz@gmail.com wrote: >> Is this whole facet just to avoid typing two extra characters in the >> case where you don't care about the keys? Using standard Ruby 1.8.4 >> without facets: >> >> foo = { :name=>"Gavin", :age=>33 } >> foo.each{ |_,v| p v } >> #=> "Gavin" >> #=> 33 > > Actually an interesting question. This is one of the earliest facets in > the library. It came out of a discussion with David Black, Matz and > others about Polymorphic behavior between Array and Hash. If we > considered a Hash's key analogous to an Array's index than one case see > how this definition of #each supports that "meshing", eg. > > x = [ :a, :b ] > x.each { |v| p v } > x = { 1 => :a, 2 => :b } > x.each { |v| p v } > > See how both array and the hash produce the same result wihtout haveing > to alter the #each statments. You can't currently do that. So this > hash#each method was created more as an "idealistic" expirement of this > concept, then for practical applicaiton --which explain why it > overrides #each vs. using each_value. > > I still think it has merit, but as an extension it does have the > potential of breaking other code. So I probably should get rid of it > --and I've known it. But I've sort of left it there as a reminder of > this interesting topic --and indeed it worked! ;-) i'm totally with the concept of hash/array interchangeability in some circumstances - but i think going the other way is easier: harp:~ > cat a.rb require 'rubygems' require 'arrayfields' (tuple = %w( ara howard 123 34 )).fields = %w( first_name last_name ssn age ) p tuple['first_name'] p tuple.values_at('ssn', 'age') tuple.each_pair{|k,v| p k => v} tuple.each{|v| p v} p tuple.join(',') harp:~ > ruby a.rb "ara" ["123", "34"] {"first_name"=>"ara"} {"last_name"=>"howard"} {"ssn"=>"123"} {"age"=>"34"} "ara" "howard" "123" "34" "ara,howard,123,43" food for thought. cheers. -a -- to foster inner awareness, introspection, and reasoning is more efficient than meditation and prayer. - h.h. the 14th dali lama