From: "David A. Black" Date: 2009-02-03T04:06:06+09:00 Subject: Re: Object#singleton_class in Ruby 1.9? Hi -- On Tue, 3 Feb 2009, Thomas Sawyer wrote: > David A. Black wrote: >> I'd be interested in seeing that article if you can find it. It >> doesn't strike me right off as a convincing idea, but the reasoning >> would be interesting to see. > > Yea I found it! > > http://blog.jayfields.com/2008/07/ruby-underuse-of-modules.html > http://blog.jayfields.com/2008/04/extend-modules-instead-of-defining.html I think it's very good advice for many situations (#extend has always been my favorite technique for changing the behavior of core objects, for example). I don't think it's necessary, though, to choose one over the other in an exclusive sense. This doesn't bother me: class C class << self def x end def y end end end etc. Jay's example with extending an object with two modules, so that the most recent one "wins" (whereas all modules "lose" if the method in question is defined in the singleton class) is very convincing, though again I wouldn't necessarily treat it as a reason never to define a method in the singleton class. I'd say it's more that you want to know what the ramifications of each technique are, and then choose the one you want. In that sense it's analogous (though not identical) to the non-singleton case, where a method defined in a class "wins", for the instances of the class, over definitions in included modules. That can be good, or not; it depends on what you're trying to do. It all comes down to mastery of the order of method lookup, which then allows you to do pretty much any permutation. David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) http://www.wishsight.com => Independent, social wishlist management!