#to_self

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.

Hi,

Have you tried? It won’t work.

          matz.

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.

On Sep 7, 9:06 am, Yukihiro M. [email protected] wrote:

Hi,

Have you tried? It won’t work.

Doh! I made that adjustment on the fly thinking it was equivalent.
Taking a step back:

class String
def to_self(obj)
obj.to_s
end
end

class Symbol
def to_self(obj)
obj.to_sym
end
end

module Grumpy
def grumpize
to_self(to_s.upcase + ‘!!!’)
end
end

class String
include Grumpy
end

class Symbol
include Grumpy
end

p “fun”.grumpize #=> “FUN!!!”
p :fun.grumpize #=> :“FUN!!!”

T.

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.

Stephen

On Sep 7, 10:52 am, Stephen C. [email protected] wrote:

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

T.

Hi,

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!!!”

On Sep 8, 6:21 pm, Nobuyoshi N. [email protected] wrote:

Why not just override Symbol#grumpy?

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.

| Privacy Policy | Terms of Service | Remote Ruby Jobs