Interger( a_symbol ) raises no exception

S

Stephan Kämper

Hi all,

yesterday at work I tried to program this: Read a simple (YAML) file
contaning key values pairs and create an object which has accessor
methods for each pair, the key name being the method name.

All went well until I realised that I'd have to treat integer values in
a slightly special way. No problem I thought, evaluate

Integer( current_value )

and if that doesn't raise an exception, go ahead...

To my surprise the Symbols dissappeared and there were ints instead.

irb(main):001:0> Integer :eek:ops
=> 23417

Why doesn't that rains an exception?
I alwasy thought Symbols were closer to Strings than Integers (resp.
Fixnums), but I might have been wrong.

What's the reason for this behaviour?

I now that there's a unique int associated to each Symbol, but I still
think that a String like "42" IS more like an Integer, than
:a_symbol_like_this.

Happy rubying

Stephan
 
M

Martin DeMello

Stephan Kämper said:
I now that there's a unique int associated to each Symbol, but I still
think that a String like "42" IS more like an Integer, than
:a_symbol_like_this.

Not to mention

irb(main):001:0> Integer :"42"
=> 15665

martin
 
E

Eric Hodel

irb(main):001:0> Integer :eek:ops
=3D> 23417

Why doesn't that rains an exception?
I alwasy thought Symbols were closer to Strings than Integers =20
(resp. Fixnums), but I might have been wrong.

What's the reason for this behaviour?

I now that there's a unique int associated to each Symbol, but I =20
still think that a String like "42" IS more like an Integer, =20
than :a_symbol_like_this.

Symbols were originally just numbers, so they have #to_int defined =20
for backwards compatibility.

--=20
Eric Hodel - (e-mail address removed) - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Interger( a_symbol ) raises no exception"

|Symbols were originally just numbers, so they have #to_int defined
|for backwards compatibility.

Until 1.8. 1.9 symbols does not have to_int any longer.

matz.
 

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,538
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top