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

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

SGksIEkgY2Fubm90IHVuZGVyc3RhbmQgaG93IGEgaGlnaCBsZXZlbCBsYW5ndWFnZSBhcyBSdWJ5
IGRvZXNuJ3QKaGFuZGxlIGEgc2ltcGxlIHRoaW5nIGFzIEMgZG9lcyB3aXRoIHN0cmluZ3M6Cgpp
biBDOgogIHN0cmluZyA9ICJBQkNERSIKICBzdHJpbmdbMF0KICA9PiAnQScKCmluIFJ1Ynk6CiAg
c3RyaW5nID0gIkFCQ0RFIgogIHN0cmluZ1swXQogID0+IDY1CgpXaHkgNjU/IGl0J3MgdGhlIEFz
Y2lpIHZhbHVlIG9mIEE6CiAgcHV0cyAiXHg2NSIKICA9PiAiZSIKClBEOiBJJ3ZlIHJlYWxpemVk
IHdoZW4gd3JpdHRpbmcgdGhpcyBtYWlsIHRoYXQgUnVieSAxLjkgYWxyZWFkeQppbXBsZW1lbnQg
dGhpcyAiZmVhdHVyZSIuCgotLSAKScOxYWtpIEJheiBDYXN0aWxsbwo8aWJjQGFsaWF4Lm5ldD4K
 
B

Boris Schmid

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
 
I

Iñaki Baz Castillo

MjAwOC81LzI2LCBCb3JpcyBTY2htaWQgPGJvcmlzQGJhZ29mc291bHMuY29tPjoKPiA+IFBEOiBJ
J3ZlIHJlYWxpemVkIHdoZW4gd3JpdHRpbmcgdGhpcyBtYWlsIHRoYXQgUnVieSAxLjkgYWxyZWFk
eQo+ICA+IGltcGxlbWVudCB0aGlzICJmZWF0dXJlIi4KPgo+ICBzdHJpbmdbMCwxXSBhbmQgc3Ry
aW5nWzBdLmNociB3aWxsIGRvIHRoZSB0cmljayBmb3IgeW91IHdoaWxlIHlvdSdyZSBpbgo+ICAx
LDgKClRoYW5rcywgZGlkbid0IGtub3cgdGhhdC4KCi0tIApJw7Fha2kgQmF6IENhc3RpbGxvCjxp
YmNAYWxpYXgubmV0Pgo=
 
R

Robert Dober

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.
HTH
Robert

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

Phil

...
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".
...
BR Phil
 
R

Robert Dober

...
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.
 
7

7stud --

Iñaki Baz Castillo said:
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."
 
R

Ryan Davis

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:
 
R

Ryan Davis

On Tue, May 27, 2008 at 7:52 AM, Ryan Davis <ryand-

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.
 

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

No members online now.

Forum statistics

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

Latest Threads

Top