From: Sean O'Halpin Date: 2008-03-09T08:55:11+09:00 Subject: Re: A question on singleton methods On Sat, Mar 8, 2008 at 11:34 PM, mike leonard wrote: > Hello, > > I have a hash full of objects, like so: > > a_hash = { :a => "alpha", :b => "beta" } > > I don't I need to define a singleton method for one of the items in > the class. I thought I could do: > > def a_hash.fetch(:a).to_s > "ALPHA" > end > > But all this gets me is a big fat error: > > t.rb:2: syntax error, unexpected tSYMBEG, expecting ')' > def a_hash.fetch(:a).to_s > ^ > > This is just a minimal example. In real life, the objects in the hash > have been generated programatically, so I don't have variable > references for them -- just the keys. > > Is it possible that there is a way around this error? If not, is there > another way to do what I'm trying to do? I'm pretty new to this, so > apologies in advance if it is an obvious mistake. > > Thank you, > > Mike Leonard > > You could try this: a_hash = { :a => "alpha", :b => "beta" } def (a_hash[:a]).to_s "ALPHA" end a_hash.each do |key, value| puts "#{key} = #{value.to_s}" end __END__ a = ALPHA b = beta Regards, Sean