Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Why "ABCDE"[0] returns an integer instead of 'A' ?

Reply
Thread Tools

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

 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      05-26-2008
SGksIEkgY2Fubm90IHVuZGVyc3RhbmQgaG93IGEgaGlnaCBsZX ZlbCBsYW5ndWFnZSBhcyBSdWJ5
IGRvZXNuJ3QKaGFuZGxlIGEgc2ltcGxlIHRoaW5nIGFzIEMgZG 9lcyB3aXRoIHN0cmluZ3M6Cgpp
biBDOgogIHN0cmluZyA9ICJBQkNERSIKICBzdHJpbmdbMF0KIC A9PiAnQScKCmluIFJ1Ynk6CiAg
c3RyaW5nID0gIkFCQ0RFIgogIHN0cmluZ1swXQogID0+IDY1Cg pXaHkgNjU/IGl0J3MgdGhlIEFz
Y2lpIHZhbHVlIG9mIEE6CiAgcHV0cyAiXHg2NSIKICA9PiAiZS IKClBEOiBJJ3ZlIHJlYWxpemVk
IHdoZW4gd3JpdHRpbmcgdGhpcyBtYWlsIHRoYXQgUnVieSAxLj kgYWxyZWFkeQppbXBsZW1lbnQg
dGhpcyAiZmVhdHVyZSIuCgotLSAKScOxYWtpIEJheiBDYXN0aW xsbwo8aWJjQGFsaWF4Lm5ldD4K

 
Reply With Quote
 
 
 
 
Boris Schmid
Guest
Posts: n/a
 
      05-26-2008
> 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/.

 
Reply With Quote
 
 
 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      05-26-2008
MjAwOC81LzI2LCBCb3JpcyBTY2htaWQgPGJvcmlzQGJhZ29mc2 91bHMuY29tPjoKPiA+IFBEOiBJ
J3ZlIHJlYWxpemVkIHdoZW4gd3JpdHRpbmcgdGhpcyBtYWlsIH RoYXQgUnVieSAxLjkgYWxyZWFk
eQo+ICA+IGltcGxlbWVudCB0aGlzICJmZWF0dXJlIi4KPgo+IC BzdHJpbmdbMCwxXSBhbmQgc3Ry
aW5nWzBdLmNociB3aWxsIGRvIHRoZSB0cmljayBmb3IgeW91IH doaWxlIHlvdSdyZSBpbgo+ICAx
LDgKClRoYW5rcywgZGlkbid0IGtub3cgdGhhdC4KCi0tIApJw7 Fha2kgQmF6IENhc3RpbGxvCjxp
YmNAYWxpYXgubmV0Pgo=

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      05-26-2008
On Mon, May 26, 2008 at 12:27 PM, I=F1aki Baz Castillo <(E-Mail Removed)> 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
> <(E-Mail Removed)>
>


HTH
Robert

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

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

 
Reply With Quote
 
Phil
Guest
Posts: n/a
 
      05-26-2008
On May 26, 1:21 pm, Robert Dober <(E-Mail Removed)> 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".

>
> ...
> > --
> > Iaki Baz Castillo
> > <(E-Mail Removed)>

>

BR Phil

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      05-26-2008
On Mon, May 26, 2008 at 4:59 PM, Phil <(E-Mail Removed)> wrote:
> On May 26, 1:21 pm, Robert Dober <(E-Mail Removed)> 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.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      05-26-2008
Just forget it, I cannot take them apart anymore, LOL, sorry for the noise.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      05-26-2008
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/.

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      05-27-2008

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:




 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      05-27-2008
On Tue, May 27, 2008 at 7:52 AM, Ryan Davis <(E-Mail Removed)> wrote:
>
> matz changed his mind for 1.9:

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorted Returns List and Reversed Returns Iterator ++imanshu Python 7 08-23-2008 04:25 AM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
block returns and hash element returns Trans Ruby 2 11-06-2005 12:15 PM



Advertisments