From: Arlen Cuss Date: 2008-03-09T08:47:12+09:00 Subject: Re: A question on singleton methods ------=_Part_42514_17792862.1205020041451 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi Mike, On Sun, Mar 9, 2008 at 10:34 AM, mike leonard wrote: > def a_hash.fetch(:a).to_s > "ALPHA" > end > The problem here is that you can't involve a method call/dispatch (or two) in the method definition. "a_hash.fetch(:a).to_s" has to make at least one method call (a_hash.fetch(:a)), and then one defines the method on the resulting object. Method definitions don't work this way (they need to be resolvable in the current context, I believe). But there's a way to get there: >> a ={:a => "q"} => {:a=>"q"} >> b = a[:a] => "q" >> a[:a].object_id => -606856898 >> b.object_id => -606856898 >> def b.some_method >> puts "Hi." >> end => nil >> b.some_method Hi. => nil >> a[:a].some_method Hi. => nil >> By the by, I'm not sure redefining a String's #to_s will have any effect. It sounds strange - perhaps you're better off building a class for those objects and defining to_s on that? Cheers, Arlen ------=_Part_42514_17792862.1205020041451--