From: "ara.t.howard" Date: 2008-05-07T23:51:44+09:00 Subject: Re: idiom for Hash like map... On May 7, 2008, at 5:18 AM, Mikel Lindsaar wrote: > class Fruit > attr_accessor :name, :price > def initialize(args) > @name = args[:name] > @price = args[:price] > end > end > > array = [ Fruit.new(:name => "apple", :price => 2), > Fruit.new(:name => "grape", :price => 20), > Fruit.new(:name => "pear", :price => 200) ] > > hash = Hash.new > > array.each do |obj| > hash.merge!({obj.name => obj.price}) > end > > hash.inspect # => "{\"grape\"=>20, \"apple\"=>2, \"pear\"=>200}" > > > > The "hash = {} / hash.merge!" thing bugs me... what is the better way? > class Fruit def to_hash { :name => name, :price => price } end end array.each{|fruit| hash.update fruit.to_hash} a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama