Why "ABCDE"[0] returns an integer instead of 'A' ?

Discussion in 'Ruby' started by Iñaki Baz Castillo, May 26, 2008.

  1. SGksIEkgY2Fubm90IHVuZGVyc3RhbmQgaG93IGEgaGlnaCBsZXZlbCBsYW5ndWFnZSBhcyBSdWJ5
    IGRvZXNuJ3QKaGFuZGxlIGEgc2ltcGxlIHRoaW5nIGFzIEMgZG9lcyB3aXRoIHN0cmluZ3M6Cgpp
    biBDOgogIHN0cmluZyA9ICJBQkNERSIKICBzdHJpbmdbMF0KICA9PiAnQScKCmluIFJ1Ynk6CiAg
    c3RyaW5nID0gIkFCQ0RFIgogIHN0cmluZ1swXQogID0+IDY1CgpXaHkgNjU/IGl0J3MgdGhlIEFz
    Y2lpIHZhbHVlIG9mIEE6CiAgcHV0cyAiXHg2NSIKICA9PiAiZSIKClBEOiBJJ3ZlIHJlYWxpemVk
    IHdoZW4gd3JpdHRpbmcgdGhpcyBtYWlsIHRoYXQgUnVieSAxLjkgYWxyZWFkeQppbXBsZW1lbnQg
    dGhpcyAiZmVhdHVyZSIuCgotLSAKScOxYWtpIEJheiBDYXN0aWxsbwo8aWJjQGFsaWF4Lm5ldD4K
    Iñaki Baz Castillo, May 26, 2008
    #1
    1. Advertising

  2. Iñaki Baz Castillo

    Boris Schmid Guest

    Re: Why "ABCDE"returns an integer instead of 'A' ?

    > PD: I've realized when writting this mail that Ruby 1.9 already
    > implement this "feature".


    string[0,1] and string[0].chr will do the trick for you while you're in
    1,8

    --
    Posted via http://www.ruby-forum.com/.
    Boris Schmid, May 26, 2008
    #2
    1. Advertising

  3. Re: Why "ABCDE"returns an integer instead of 'A' ?

    MjAwOC81LzI2LCBCb3JpcyBTY2htaWQgPGJvcmlzQGJhZ29mc291bHMuY29tPjoKPiA+IFBEOiBJ
    J3ZlIHJlYWxpemVkIHdoZW4gd3JpdHRpbmcgdGhpcyBtYWlsIHRoYXQgUnVieSAxLjkgYWxyZWFk
    eQo+ICA+IGltcGxlbWVudCB0aGlzICJmZWF0dXJlIi4KPgo+ICBzdHJpbmdbMCwxXSBhbmQgc3Ry
    aW5nWzBdLmNociB3aWxsIGRvIHRoZSB0cmljayBmb3IgeW91IHdoaWxlIHlvdSdyZSBpbgo+ICAx
    LDgKClRoYW5rcywgZGlkbid0IGtub3cgdGhhdC4KCi0tIApJw7Fha2kgQmF6IENhc3RpbGxvCjxp
    YmNAYWxpYXgubmV0Pgo=
    Iñaki Baz Castillo, May 26, 2008
    #3
  4. Iñaki Baz Castillo

    Robert Dober Guest

    On Mon, May 26, 2008 at 12:27 PM, I=F1aki Baz Castillo <> wrot=
    e:
    > Hi, I cannot understand how a high level language as Ruby doesn't
    > handle a simple thing as C does with strings:
    >
    > in C:
    > string =3D "ABCDE"
    > string[0]
    > =3D> 'A'
    >
    > in Ruby:
    > string =3D "ABCDE"
    > string[0]
    > =3D> 65

    Maybe because
    a) in C 'A' =3D=3D 65
    b) Ruby has different ways to get substrings, x[0,1] or x[0..0]
    c) Matz thought it was a good idea ;)
    d) it makes lots of sense, unknown paradigms are not necessarily worse
    than know ones.

    However, IIRC Ruby1.9 will as you have said below tell the contrary :(
    >
    > Why 65? it's the Ascii value of A:
    > puts "\x65"
    > =3D> "e"

    because of
    "e"[0] =3D=3D ?e && ?e =3D=3D 0x65,
    what did you want to do with \x?
    >
    > PD: I've realized when writting this mail that Ruby 1.9 already
    > implement this "feature".

    Yup, obviously too many people were puzzled by this.
    >
    > --
    > I=F1aki Baz Castillo
    > <>
    >


    HTH
    Robert

    --=20
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
    Robert Dober, May 26, 2008
    #4
  5. Iñaki Baz Castillo

    Phil Guest

    On May 26, 1:21 pm, Robert Dober <> wrote:
    > ...
    >
    > > Why 65? it's the Ascii value of A:
    > > puts "\x65"
    > > => "e"

    >
    > because of
    > "e"[0] == ?e && ?e == 0x65,
    > what did you want to do with \x?


    \x is for hexadezimal. 65 is indeed the (decimal) Ascii value of 'A'.
    hex = dez
    0x65 = 101
    0x41 = 65

    And therefore:
    puts "\x41" => 'A'
    puts "\x65" => 'e'

    >
    > > PD: I've realized when writting this mail that Ruby 1.9 already
    > > implement this "feature".

    >
    > ...
    > > --
    > > Iñaki Baz Castillo
    > > <>

    >

    BR Phil
    Phil, May 26, 2008
    #5
  6. Iñaki Baz Castillo

    Robert Dober Guest

    On Mon, May 26, 2008 at 4:59 PM, Phil <> wrote:
    > On May 26, 1:21 pm, Robert Dober <> wrote:
    >> ...
    >>
    >> > Why 65? it's the Ascii value of A:
    >> > puts "\x65"
    >> > => "e"

    >>
    >> because of
    >> "e"[0] == ?e && ?e == 0x65,
    >> what did you want to do with \x?

    >
    > \x is for hexadezimal. 65 is indeed the (decimal) Ascii value of 'A'.
    > hex = dez
    > 0x65 = 101
    > 0x41 = 65
    >

    Thanks for correcting my error Philip
    I should have written

    "A"[0] etc.etc.

    I got confused by the => e

    R.
    Robert Dober, May 26, 2008
    #6
  7. Iñaki Baz Castillo

    Robert Dober Guest

    Just forget it, I cannot take them apart anymore, LOL, sorry for the noise.
    Robert Dober, May 26, 2008
    #7
  8. Iñaki Baz Castillo

    7stud -- Guest

    Re: Why "ABCDE"returns an integer instead of 'A' ?

    Iñaki Baz Castillo wrote:
    > Hi, I cannot understand how a high level language as Ruby doesn't
    > handle a simple thing as C does with strings:
    >
    > in C:
    > string = "ABCDE"
    > string[0]
    > => 'A'
    >
    > in Ruby:
    > string = "ABCDE"
    > string[0]
    > => 65
    >
    > Why 65? it's the Ascii value of A:
    > puts "\x65"
    > => "e"
    >


    Repeat after me: "Ruby does not get in your way. Ruby does not get in
    your way."

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, May 26, 2008
    #8
  9. Iñaki Baz Castillo

    Ryan Davis Guest

    On May 26, 2008, at 04:21 , Robert Dober wrote:

    > c) Matz thought it was a good idea ;)


    matz changed his mind for 1.9:


    > % multiruby -e 'p "a"[0]'
    > VERSION = 1.8.4
    > CMD = ~/.multiruby/install/1.8.4/bin/ruby -e 'p "a"[0]'
    >
    > 97
    >
    > RESULT = 0
    >
    > VERSION = 1.8.5
    > CMD = ~/.multiruby/install/1.8.5/bin/ruby -e 'p "a"[0]'
    >
    > 97
    >
    > RESULT = 0
    >
    > VERSION = 1.8.6-p114
    > CMD = ~/.multiruby/install/1.8.6-p114/bin/ruby -e 'p "a"[0]'
    >
    > 97
    >
    > RESULT = 0
    >
    > VERSION = 1.9.0-1
    > CMD = ~/.multiruby/install/1.9.0-1/bin/ruby -e 'p "a"[0]'
    >
    > "a"


    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    > RESULT = 0
    >
    > VERSION = rubinius
    > CMD = ~/.multiruby/install/rubinius/shotgun/rubinius -e 'p "a"[0]'
    >
    > 97
    >
    > RESULT = 0
    >
    > TOTAL RESULT = 0 failures out of 5
    >
    > Passed: 1.8.4, 1.8.5, rubinius, 1.8.6-p114, 1.9.0-1
    > Failed:
    Ryan Davis, May 27, 2008
    #9
  10. Iñaki Baz Castillo

    Robert Dober Guest

    On Tue, May 27, 2008 at 7:52 AM, Ryan Davis <> wrote:
    >
    > matz changed his mind for 1.9:

    I hope his new one will be as brilliant as was his old one !
    R.
    Robert Dober, May 27, 2008
    #10
  11. Iñaki Baz Castillo

    Ryan Davis Guest

    On May 27, 2008, at 00:17 , Robert Dober wrote:

    > On Tue, May 27, 2008 at 7:52 AM, Ryan Davis <ryand-
    > > wrote:
    >>
    >> matz changed his mind for 1.9:

    > I hope his new one will be as brilliant as was his old one !


    yeah... well... in this case, his new mind breaks more of my code than
    anything else.
    Ryan Davis, May 27, 2008
    #11
    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. vizlab
    Replies:
    3
    Views:
    4,197
    Michael Bar-Sinai
    Oct 17, 2007
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    877
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,806
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    33
    Views:
    872
  5. ++imanshu
    Replies:
    7
    Views:
    468
    ++imanshu
    Aug 23, 2008
Loading...

Share This Page