From: "Thomas B." Date: 2008-08-30T19:28:32+09:00 Subject: Re: assignment operator that treats empty and nil as the same thing Xavier Noria wrote: > On Sat, Aug 30, 2008 at 5:00 AM, RichardOnRails > wrote: > >> Maybe I can redeem myself: Matt gave you a great solution in reopening >> the Hash definition. I recommend a minor improvement as follows: >> >> class Hash >> alias_method "orig_fetch", "[]" >> def [](what) >> result = orig_fetch(what) >> result = nil if result == '' >> end >> end > > In my view that's abusing open classes. Ruby has well-defined boolean > semantics, and Hash#[] is a fundamental method. People have > expectations about those ones. I agree with that. But if it's just one hash object that causes you problems, you can always change the method in the object's eigenclass: class << your_hash_object alias_method "origfetch", "[]" def [](what) result = origfetch(what) if result == '' nil else result end end end This way you won't break the existing functionality for other hashes. Or you could simply extend the class, modify the method in the descendant class, and make all your hashes instances of the descendant class. TPR. -- Posted via http://www.ruby-forum.com/.