From: Robert Klemme Date: 2011-01-05T23:35:38+09:00 Subject: Re: Is singleton class of an object already created? On Wed, Jan 5, 2011 at 11:24 AM, Adam Prescott wrote: > On Tue, Jan 4, 2011 at 9:00 PM, timr wrote: > >> class Object >>  def singleton_class? >>    self.singleton_methods.length == 0 ? false : true >>  end >> end >> > > Really small point here, and not really on topic, but this is a little > around the houses and can be slightly improved: I couldn't agree more! Using ternary operator with "false" and "true" is not needed most of the time. (Only exception I would concede is to avoid leakage of references - but then you can as well do "!! expr".) > class Object >  def singleton_class? >    !self.singleton_methods.empty? >  end > end > > Or just != 0, since it'll return the true/false without the need for a > ternary. Or just class Object def singleton_class? !singleton_methods.empty? end end Now we can wonder whether this is worth a method at all... :-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/