Why "ABCDE"[0] 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".

···

--
Iñaki Baz Castillo
<ibc@aliax.net>

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

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

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 Castillo
<ibc@aliax.net>

HTH
Robert

···

On Mon, May 26, 2008 at 12:27 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:

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

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

* Iñaki Baz Castillo, 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

···

--
Blog available at http://cip.physik.uni-bonn.de/~jupp/
PGP key with id 6CC6574F available at http://wwwkeys.de.pgp.net/
Jabber - http://www.jabber.org/ - contact information on request

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

Thanks, didn't know that.

···

2008/5/26, Boris Schmid <boris@bagofsouls.com>:

> 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ñaki Baz Castillo
<ibc@aliax.net>

...

> 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
> <i...@aliax.net>

BR Phil

···

On May 26, 1:21 pm, Robert Dober <robert.do...@gmail.com> wrote:

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

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"

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

···

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

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:

Thanks for correcting my error Philip
I should have written

"A"[0] etc.etc.

I got confused by the => e

R.

···

On Mon, May 26, 2008 at 4:59 PM, Phil <phil_meier@gmx.net> wrote:

On May 26, 1:21 pm, Robert Dober <robert.do...@gmail.com> 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

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

···

On Tue, May 27, 2008 at 7:52 AM, Ryan Davis <ryand-ruby@zenspider.com> wrote:

matz changed his mind for 1.9:

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

yeah... well... in this case, his new mind breaks more of my code than anything else.

···

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

On Tue, May 27, 2008 at 7:52 AM, Ryan Davis <ryand- > ruby@zenspider.com> wrote:

matz changed his mind for 1.9:

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