String Element Reference

Discussion in 'Ruby' started by jason solomon, May 25, 2011.

  1. I am new to Ruby so I apologize if this seems dumb.

    I am currently running ruby 1.8.7 (2009-06-12 patchlevel 174)
    [universal-darwin10.0].

    When working in an irb session I'm having trouble with String element
    reference.

    Example:

    a = "hello"

    a[0] should return "h", but instead is returning 104.

    Seems like it's return the ascii value of the element, but why?


    When I do a[0,3] the return is the expected "hel".




    Any ideas as to why a[0] is returning a FixNumb object?

    Ideas / help would be greatly appreciated.

    --
    Posted via http://www.ruby-forum.com/.
    jason solomon, May 25, 2011
    #1
    1. Advertising

  2. On 25.05.2011 19:23, jason solomon wrote:
    > I am new to Ruby so I apologize if this seems dumb.
    >
    > I am currently running ruby 1.8.7 (2009-06-12 patchlevel 174)
    > [universal-darwin10.0].
    >
    > When working in an irb session I'm having trouble with String element
    > reference.
    >
    > Example:
    >
    > a = "hello"
    >
    > a[0] should return "h", but instead is returning 104.
    >
    > Seems like it's return the ascii value of the element, but why?
    >
    >
    > When I do a[0,3] the return is the expected "hel".
    >
    >
    >
    >
    > Any ideas as to why a[0] is returning a FixNumb object?
    >
    > Ideas / help would be greatly appreciated.


    This has changed in Ruby 1.9. You probably read 1.9 documentation
    that's why you expect a[0] to return "h".

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, May 25, 2011
    #2
    1. Advertising

  3. jason solomon, May 25, 2011
    #3
  4. jason solomon

    7stud -- Guest

    jason solomon wrote in post #1000974:
    > I am new to Ruby so I apologize if this seems dumb.
    >
    > I am currently running ruby 1.8.7 (2009-06-12 patchlevel 174)
    > [universal-darwin10.0].
    >
    > When working in an irb session I'm having trouble with String element
    > reference.
    >
    > Example:
    >
    > a = "hello"
    >
    > a[0] should return "h", but instead is returning 104.
    >
    > Seems like it's return the ascii value of the element, but why?
    >
    >
    > When I do a[0,3] the return is the expected "hel".
    >
    >
    >
    >
    > Any ideas as to why a[0] is returning a FixNumb object?
    >


    For some reason Matz thought it was more useful to define the [] method
    for strings to return the ascii value. As you discovered, if you
    specify a length as the second argument, you get the characters.
    Applying that knowledge, if you specify a length of 1, you will get the
    character. So the expression:

    str[0,1]

    will return the character in both ruby 1.8 and ruby 1.9.

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, May 25, 2011
    #4
  5. That is the way String indexing works in Ruby=2C if you only pass one param=
    eter you'll get the ASCII value of the character. If you would like to get =
    "h" you must do a[0=2C1] where the first parameter tells the parser to get=
    the first character of the String and the second parameter tells the numbe=
    r of characters you want to get from that position.
    Hope this helps
    Javier Hidalgo




    > Date: Thu=2C 26 May 2011 02:23:38 +0900
    > From:
    > Subject: String Element Reference
    > To:
    >=20
    > I am new to Ruby so I apologize if this seems dumb.
    >=20
    > I am currently running ruby 1.8.7 (2009-06-12 patchlevel 174)
    > [universal-darwin10.0].
    >=20
    > When working in an irb session I'm having trouble with String element
    > reference.
    >=20
    > Example:
    >=20
    > a =3D "hello"
    >=20
    > a[0] should return "h"=2C but instead is returning 104.
    >=20
    > Seems like it's return the ascii value of the element=2C but why?
    >=20
    >=20
    > When I do a[0=2C3] the return is the expected "hel".
    >=20
    >=20
    >=20
    >=20
    > Any ideas as to why a[0] is returning a FixNumb object?
    >=20
    > Ideas / help would be greatly appreciated.
    >=20
    > --=20
    > Posted via http://www.ruby-forum.com/.
    >=20

    =
    Javier Hidalgo Villegas, May 25, 2011
    #5
    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. MSNews
    Replies:
    1
    Views:
    2,720
    Alvin Bruney [MVP]
    Apr 22, 2004
  2. Rod  Snyder

    moving from form element to form element

    Rod Snyder, May 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    405
    Vidar Petursson
    May 29, 2004
  3. Fred
    Replies:
    5
    Views:
    617
    peter koch
    Feb 7, 2007
  4. HANM
    Replies:
    2
    Views:
    706
    Joseph Kesselman
    Jan 29, 2008
  5. Victor Hooi
    Replies:
    4
    Views:
    309
Loading...

Share This Page