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

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”

PD: I’ve realized when writting this mail that Ruby 1.9 already
implement this “feature”.

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

2008/5/26, Boris S. [email protected]:

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

Thanks, didn’t know that.

On Mon, May 26, 2008 at 12:27 PM, Iñaki Baz C. [email protected] wrote:

string[0]
=> 65
Maybe because
a) in C ‘A’ == 65
b) Ruby has different ways to get substrings, x[0,1] or x[0…0]
c) Matz thought it was a good idea :wink:
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 :frowning:

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?

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ñaki Baz C.
[email protected]

HTH
Robert


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


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

  • Iñaki Baz C., 2008-05-26, 19:27:

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’

$ echo “#include <stdio.h>\nint main() { return ‘A’;}” > a.c;
cc a.c; ./a.out; echo $?
65

Josef ‘Jupp’ Schugt

On May 26, 1:21 pm, Robert D. [email protected] 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 C.
[email protected]

BR Phil

On Mon, May 26, 2008 at 4:59 PM, Phil [email protected] wrote:

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

Just forget it, I cannot take them apart anymore, LOL, sorry for the
noise.

Iñaki Baz C. 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.”

On Tue, May 27, 2008 at 7:52 AM, Ryan D. [email protected]
wrote:

matz changed his mind for 1.9:
I hope his new one will be as brilliant as was his old one !
R.

On May 26, 2008, at 04:21 , Robert D. wrote:

c) Matz thought it was a good idea :wink:

matz changed his mind for 1.9:

VERSION = 1.9.0-1
CMD = ~/.multiruby/install/1.9.0-1/bin/ruby -e ‘p “a”[0]’

“a”

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

On May 27, 2008, at 00:17 , Robert D. wrote:

On Tue, May 27, 2008 at 7:52 AM, Ryan D. <ryand-
[email protected]> 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.