How is Symbol#to_int useful? (should it exist?)

P

Phil Tomson

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
 
E

Eric Hodel

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?

Symbols used to be just numbers without names attached.
 
N

nobu

Hi,

At Fri, 24 Mar 2006 18:33:53 +0900,
Phil Tomson 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top