From: Yukihiro Matsumoto Date: 2008-09-07T22:06:04+09:00 Subject: Re: #to_self Hi, Have you tried? It won't work. matz. In message "Re: #to_self" on Sun, 7 Sep 2008 21:48:30 +0900, Trans writes: | |Say I have a mixin: | | module Grumpy | def grumpize | to_s.upcase + '!!!!!!!!!' | end | end | | class String | include Grumpy | end | | "fun".grumpize #=> "FUN!!!!!!!!!" | |Now, | | class Symbol | include Grumpy | end | | :fun.grumpize #=> "FUN!!!!!!!!!" | |But, hey, I want a Grumpy Symbol here, not a String! | |I don't want to have to make a mixin exception for Symbol. So what's a |general solution? | |My considerations of this led to: | | class String | alias_method :to_self, :to_s | end | | class Symbol | alias_method :to_self, :to_sym | end | | module Grumpy | def grumpize | (to_s.upcase + '!!!!!!!!!').to_self | end | end | |That does the trick. And more besides, #to_self seems interesting in |and of itself. Would #to_self be generally useful throughout Ruby's |Object models? What else could be done with it, I wonder? | |T.