--wBQTnIN5ZprSPVev3x2
Content-Type: multipart/alternative; boundary="=-N5OB/TqoVbB9w56qxQNu"


--5OB/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 = %w{ ant cat dog }
> I wrote
> a = %{ 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 = %{ ant cat dog }
=> " ant cat dog "
irb(main):002:0> a[2]
=> 110
irb(main):003:0> a = %w{ ant cat dog }
=> ["ant", "cat", "dog"]
irb(main):004:0> a[2]
=> "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".

-- 
Michael T. Richter <ttmrichter / gmail.com> (GoogleTalk:
ttmrichter / gmail.com)
Those who have learned from history are bound to helplessly watch it
repeat itself. (Albert Y. C. Lai)

--5OB/TqoVbB9w56qxQNu
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
  <META NAME="GENERATOR" CONTENT="GtkHTML/3.16.1">
</HEAD>
<BODY>
<BR>
On Wed, 2008-05-07 at 20:10 +0900, VICTOR GOLDBERG wrote:
<BLOCKQUOTE TYPE=CITE>
<PRE>
<FONT COLOR="#000000">Instead of writing</FONT>
<FONT COLOR="#000000">a = %w{ ant cat dog }</FONT>
<FONT COLOR="#000000">I wrote</FONT>
<FONT COLOR="#000000">a = %{ ant cat dog }</FONT>
<FONT COLOR="#000000">puts a[2]        --&gt;        110</FONT>
</PRE>
</BLOCKQUOTE>
<PRE>

</PRE>
<BLOCKQUOTE TYPE=CITE>
<PRE>
<FONT COLOR="#000000">I didn't find an explanation for this result in Dave Thomas' book</FONT>
<FONT COLOR="#000000">Anybody volunteers a response?</FONT>
</PRE>
</BLOCKQUOTE>
<BR>
irb(main):001:0&gt; a = %{ ant cat dog }<BR>
=&gt; &quot; ant cat dog &quot;<BR>
irb(main):002:0&gt; a[2]<BR>
=&gt; 110<BR>
irb(main):003:0&gt; a = %w{ ant cat dog }<BR>
=&gt; [&quot;ant&quot;, &quot;cat&quot;, &quot;dog&quot;]<BR>
irb(main):004:0&gt; a[2]<BR>
=&gt; &quot;dog&quot;<BR>
<BR>
They're different types.&nbsp; The first one came back as the string &quot;nt cat dog &quot; while the second came back as an array with three strings [&quot;ant&quot;,&quot;cat&quot;,&quot;dog&quot;].&nbsp; 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.&nbsp; (I may be using a bit of sarcasm with that &quot;principle of least surprise&quot; thing there....)&nbsp; So a[0] is 32 (the ASCII encoding for a space), a[1] is 97 (for &quot;a&quot;) and a[2] is 110 (for &quot;n&quot;).&nbsp; If you do it the right way (for your purposes, I mean) a[0] is &quot;ant&quot;, a[1] is &quot;cat&quot; and a[2] is &quot;dog&quot;.<BR>
<BR>
<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
-- <BR>
<B>Michael T. Richter</B> &lt;ttmrichter / gmail.com&gt; (<B>GoogleTalk:</B> ttmrichter / gmail.com)<BR>
<I>Those who have learned from history are bound to helplessly watch it repeat itself. (Albert Y. C. Lai)</I>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

--5OB/TqoVbB9w56qxQNu--

--wBQTnIN5ZprSPVev3x2
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
GY
-----END PGP SIGNATURE-----

--wBQTnIN5ZprSPVev3x2--