Interger( a_symbol ) raises no exception

Discussion in 'Ruby' started by Stephan Kämper, Nov 11, 2005.

  1. 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
     
    Stephan Kämper, Nov 11, 2005
    #1
    1. Advertising

  2. Stephan Kämper <> wrote:
    >
    > 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
     
    Martin DeMello, Nov 11, 2005
    #2
    1. Advertising

  3. Stephan Kämper

    Eric Hodel Guest

    On Nov 10, 2005, at 10:52 PM, Stephan K=E4mper wrote:

    > 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 - - http://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
     
    Eric Hodel, Nov 11, 2005
    #3
  4. Hi,

    In message "Re: Interger( a_symbol ) raises no exception"
    on Fri, 11 Nov 2005 18:04:45 +0900, Eric Hodel <> writes:

    |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.
     
    Yukihiro Matsumoto, Nov 11, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Cannot be converted to 'Interger'

    =?Utf-8?B?VGltOjouLg==?=, Nov 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,701
    chanmmn
    Nov 3, 2004
  2. Noah
    Replies:
    3
    Views:
    505
  3. Trond Valen
    Replies:
    20
    Views:
    1,185
    Jacek Dziedzic
    Dec 7, 2005
  4. Fredrik Lundh
    Replies:
    0
    Views:
    421
    Fredrik Lundh
    Jul 23, 2008
  5. Tim Chase
    Replies:
    1
    Views:
    1,518
    Sean DiZazzo
    Jul 13, 2009
Loading...

Share This Page