From: "Michael T. Richter" Date: 2008-05-07T20:38:53+09:00 Subject: Re: Interesting result of a newbie mistake --=-xwBQTnIN5ZprSPVev3x2 Content-Type: multipart/alternative; boundary="=-N5OB/TqoVbB9w56qxQNu" --=-N5OB/TqoVbB9w56qxQNu Content-Type: text/plain Content-Transfer-Encoding: quoted-printable On Wed, 2008-05-07 at 20:10 +0900, VICTOR GOLDBERG wrote: > Instead of writing > a =3D %w{ ant cat dog } > I wrote > a =3D %{ ant cat dog } > puts a[2] --> 110 > I didn't find an explanation for this result in Dave Thomas' book > Anybody volunteers a response? irb(main):001:0> a =3D %{ ant cat dog } =3D> " ant cat dog " irb(main):002:0> a[2] =3D> 110 irb(main):003:0> a =3D %w{ ant cat dog } =3D> ["ant", "cat", "dog"] irb(main):004:0> a[2] =3D> "dog" They're different types. The first one came back as the string " ant cat dog " while the second came back as an array with three strings ["ant","cat","dog"]. One of the principles of least surprise in Ruby is that indexing a string gives you the integer value of the character at that location. (I may be using a bit of sarcasm with that "principle of least surprise" thing there....) So a[0] is 32 (the ASCII encoding for a space), a[1] is 97 (for "a") and a[2] is 110 (for "n"). If you do it the right way (for your purposes, I mean) a[0] is "ant", a[1] is "cat" and a[2] is "dog". --=20 Michael T. Richter (GoogleTalk: ttmrichter@gmail.com) Those who have learned from history are bound to helplessly watch it repeat itself. (Albert Y. C. Lai) --=-N5OB/TqoVbB9w56qxQNu Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Wed, 2008-05-07 at 20:10 +0900, VICTOR GOLDBERG wrote:
Instead of writing
a =3D %w{ ant cat dog }
I wrote
a =3D %{ ant cat dog }
puts a[2]        -->        110

I didn't find an explanation for this result in Dav=
e Thomas' book
Anybody volunteers a response?

irb(main):001:0> a =3D %{ ant cat dog }
=3D> " ant cat dog "
irb(main):002:0> a[2]
=3D> 110
irb(main):003:0> a =3D %w{ ant cat dog }
=3D> ["ant", "cat", "dog"]
irb(main):004:0> a[2]
=3D> "dog"

They're different types.  The first one came back as the string "= ant cat dog " while the second came back as an array with three strin= gs ["ant","cat","dog"].  One of the prin= ciples of least surprise in Ruby is that indexing a string gives you the in= teger value of the character at that location.  (I may be using a bit = of sarcasm with that "principle of least surprise" thing there...= .)  So a[0] is 32 (the ASCII encoding for a space), a[1] is 97 (for &q= uot;a") and a[2] is 110 (for "n").  If you do it the ri= ght way (for your purposes, I mean) a[0] is "ant", a[1] is "= cat" and a[2] is "dog".

--
Michael T. Richter <ttmri= chter@gmail.com> (GoogleTalk: ttmrichter@gmail.com)
Those who have learned from history are bound to helplessly watch it rep= eat itself. (Albert Y. C. Lai)
--=-N5OB/TqoVbB9w56qxQNu-- --=-xwBQTnIN5ZprSPVev3x2 Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQBIIZS/LqyWkKVQ54QRAmOxAJ4t2cv1aeS2ylflQHeJn5br6M1v6QCfRRJk SjMrJNtfg7R4wf6CyKRcegE= =xAGY -----END PGP SIGNATURE----- --=-xwBQTnIN5ZprSPVev3x2--