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?
In message “Re: #to_self”
on Sun, 7 Sep 2008 21:48:30 +0900, Trans [email protected]
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.
self.to_self(self.to_s.upcase + ‘!!!’)
end
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
At Mon, 8 Sep 2008 00:35:34 +0900,
Trans wrote in [ruby-talk:314145]:
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:
Why not just override Symbol#grumpy?
module Grumpy
def grumpize
“#{to_s.upcase}!!!”
end
end
class String
include Grumpy
end
class Symbol
include Grumpy
def grumpize
super.to_sym
end
end
p “fun”.grumpize #=> “FUN!!!”
p :fun.grumpize #=> :“FUN!!!”
Or, String#try_convert is defined in 1.9:
module Grumpy
def grumpize
self.class.try_convert("#{to_s.upcase}!!!")
end
end
class String
include Grumpy
end
class Symbol
include Grumpy
def self.try_convert(s)
s.to_sym
end
end
p “fun”.grumpize #=> “FUN!!!”
p :fun.grumpize #=> :“FUN!!!”
class Symbol
include Grumpy
def grumpize
super.to_sym
end
end
p “fun”.grumpize #=> “FUN!!!”
p :fun.grumpize #=> :“FUN!!!”
Easy enough when you have one method. No fun with 20+.
end
class Symbol
include Grumpy
def self.try_convert(s)
s.to_sym
end
end
p “fun”.grumpize #=> “FUN!!!”
p :fun.grumpize #=> :“FUN!!!”
Ah, now that’s interesting, and more along the lines of what I’ve been
pondering. This is like #to_self defined on the metaclass, which makes
sense. And is perfect use if limited to particular conversions (like
String/Symbol conversion). What is the intended purpose of #try_convert, BTW, ie. Why was it added to 1.9?
My case still calls for a special method though, if it is to be open
for use for anything more than String/Symbol. The Integer example for
instance --a try_convert of #to_i here would be pointless for
Grumpies. The use of the special method is basically the same concept
as for Hash#<< on ruby-core where Enumerable could use an optional
enumerator/species method. Seems almost like some sort of Design
Pattern.
T.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.