# How is Symbol#to\_int useful? (should it exist?)

**URL:** <https://www.ruby-forum.com/t/how-is-symbol-to-int-useful-should-it-exist/86706>\
**Category:** Ruby\
**Created:** [March 24, 2006, 10:34am UTC](https://www.ruby-forum.com/t/how-is-symbol-to-int-useful-should-it-exist/86706 "2006-03-24T10:34:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 24, 2006, 10:34am UTC](https://www.ruby-forum.com/t/how-is-symbol-to-int-useful-should-it-exist/86706/1 "2006-03-24T10:34:02Z")

</div>

I had a situation in a DSL like so:

str = ‘0’\*n

Where n in one context could be a Symbol. If n happened to be a Symbol,  
I  
wanted to get an error message. However, I found that if n is a symbol  
you  
could get a very long string of 0’s because there is a Symbol#to\_int  
method  
which returns a unique integer for each Symbol object (actually, it  
calls  
Symbol#to\_i). And so I don’t get the error message I want to get  
because the  
symbol gets converted to an Integer automatically. So what’s the  
usefulness of  
the Symbol#to\_int method? I’m just undefining it like so:

class Symbol  
undef\_method :to\_int  
end

…that works for my purposes, but I wonder why there is a  
Symbol#to\_int method in the library. Why not just require that  
Symbol#to\_i be  
called explicitly if that’s what you want?

Phil

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 24, 2006, 12:18pm UTC](https://www.ruby-forum.com/t/how-is-symbol-to-int-useful-should-it-exist/86706/2 "2006-03-24T12:18:50Z")

</div>

On Mar 24, 2006, at 1:33 AM, Phil T. wrote:

> which returns a unique integer for each Symbol object (actually, it
> 
> …that works for my purposes, but I wonder why there is a  
> Symbol#to\_int method in the library. Why not just require that  
> Symbol#to\_i be  
> called explicitly if that’s what you want?

Symbols used to be just numbers without names attached.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 24, 2006, 12:49pm UTC](https://www.ruby-forum.com/t/how-is-symbol-to-int-useful-should-it-exist/86706/3 "2006-03-24T12:49:19Z")

</div>

Hi,

At Fri, 24 Mar 2006 18:33:53 +0900,  
Phil T. wrote in [ruby-talk:185669]:

> class Symbol  
> undef\_method :to\_int  
> end
> 
> …that works for my purposes, but I wonder why there is a  
> Symbol#to\_int method in the library. Why not just require that Symbol#to\_i be  
> called explicitly if that’s what you want?

Histrical reason. Formerly, until Symbol class was introduced  
Fixnum was used. Symbol#to\_int is deprecated and has been  
purged in 1.9.

$ ruby1.8 -ve :foo.to\_int  
ruby 1.8.4 (2005-12-24) [i486-linux]  
-e:1: warning: treating Symbol as an integer
