# \#to\_self

**URL:** <https://www.ruby-forum.com/t/to-self/147948>\
**Category:** Ruby\
**Created:** [September 7, 2008, 2:55pm UTC](https://www.ruby-forum.com/t/to-self/147948 "2008-09-07T14:55:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [September 7, 2008, 2:55pm UTC](https://www.ruby-forum.com/t/to-self/147948/1 "2008-09-07T14:55:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [September 7, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/to-self/147948/2 "2008-09-07T15:12:46Z")

</div>

Hi,

Have you tried? It won’t work.

```
          matz.

```

In message “Re: #to\_self”  
on Sun, 7 Sep 2008 21:48:30 +0900, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [September 7, 2008, 4:13pm UTC](https://www.ruby-forum.com/t/to-self/147948/3 "2008-09-07T16:13:41Z")

</div>

On Sep 7, 9:06 am, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [September 7, 2008, 4:58pm UTC](https://www.ruby-forum.com/t/to-self/147948/4 "2008-09-07T16:58:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [September 7, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/to-self/147948/5 "2008-09-07T17:42:17Z")

</div>

On Sep 7, 10:52 am, Stephen C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [September 9, 2008, 1:00am UTC](https://www.ruby-forum.com/t/to-self/147948/6 "2008-09-09T01:00:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [September 9, 2008, 3:36am UTC](https://www.ruby-forum.com/t/to-self/147948/7 "2008-09-09T03:36:34Z")

</div>

On Sep 8, 6:21 pm, Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
