From: Trans Date: 2008-09-08T00:35:34+09:00 Subject: Re: #to_self On Sep 7, 10:52 am, Stephen Celis wrote: > Hi, > > On Sep 7, 2008, at 9:05 AM, Trans wrote: > > > def grumpize > >   to_self(to_s.upcase + '!!!!!!!!!') > > end > > This is a bit of a confusing idiom, especially when expanded: > >    self.to_self(self.to_s.upcase + '!!!!!!!!!') > > You may make yourself slightly clearer with a block method. A   > simplified example: > >    class Object >      def returning_initial_class(&block) >        yield(self).__send__ case self >          when String then :to_s >          when Symbol then :to_sym >        end >      end >    end > >    module Grumpy >      def grumpize >        returning_initial_class { |grump| grump.to_s.upcase +   > '!!!!!!!!!' } >      end >    end > > It's cleanest to just define String#grumpize and Symbol#grumpize   > individually. Ah, that helps me "cleanest" my thoughts. It occurs to me that while #to_self() makes sense in this context, it may differ depending on the mixin used. For example: class Integer include Grumpy end That could work, but if to_self is used there will be issues clearly (!!!!!!!!! ain't a number). Hence, what I'm really after is an optional method that can be defined when needed to say "how to become grumpy". Then: module Grumpy def make_grumpy(obj) obj end def grumpize make_grumpy(to_s.upcase + '!!!!!!!!!') end end class Symbol include Grumpy def make_grumpy(obj) obj.to_sym end end T.