# Interesting result of a newbie mistake

**URL:** <https://www.ruby-forum.com/t/interesting-result-of-a-newbie-mistake/136925>\
**Category:** Ruby\
**Created:** [May 7, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/interesting-result-of-a-newbie-mistake/136925 "2008-05-07T13:11:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![VICTOR\_GOLDBERG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/90ced4/32.png) [@VICTOR\_GOLDBERG](https://www.ruby-forum.com/u/VICTOR_GOLDBERG)\
**Post date:** [May 7, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/interesting-result-of-a-newbie-mistake/136925/1 "2008-05-07T13:11:35Z")

</div>

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 T.’ book  
Anybody volunteers a response?

Thanks,  
Víctor

---

<div class="post-metadata">

**Author:** ![VICTOR\_GOLDBERG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/90ced4/32.png) [@VICTOR\_GOLDBERG](https://www.ruby-forum.com/u/VICTOR_GOLDBERG)\
**Post date:** [May 7, 2008, 1:18pm UTC](https://www.ruby-forum.com/t/interesting-result-of-a-newbie-mistake/136925/2 "2008-05-07T13:18:02Z")

</div>

2008/5/7 VICTOR GOLDBERG [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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 T.’ book  
> Anybody volunteers a response?

irb(main):001:0\> a = %{ ant cat dog }  
=\> " ant cat dog "  
irb(main):002:0\> a.class  
=\> String  
irb(main):003:0\> a[2]  
=\> 110  
irb(main):004:0\> a[2].chr  
=\> “n”

Cheers

robert

---

<div class="post-metadata">

**Author:** ![VICTOR\_GOLDBERG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/90ced4/32.png) [@VICTOR\_GOLDBERG](https://www.ruby-forum.com/u/VICTOR_GOLDBERG)\
**Post date:** [May 7, 2008, 1:37pm UTC](https://www.ruby-forum.com/t/interesting-result-of-a-newbie-mistake/136925/3 "2008-05-07T13:37:38Z")

</div>

When you have somthing like

a = %{ ant cat dog }  
=\> " ant cat dog "  
irb(main):002:0\> a.class  
=\> String  
irb(main):003:0\> a[2]  
=\> 110  
irb(main):004:0\> a[2].chr  
=\> “n”

it displaying the ascii value of that character at that paticular place

cheers,  
kranthi

On Wed, May 7, 2008 at 4:45 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![VICTOR\_GOLDBERG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/90ced4/32.png) [@VICTOR\_GOLDBERG](https://www.ruby-forum.com/u/VICTOR_GOLDBERG)\
**Post date:** [May 7, 2008, 1:39pm UTC](https://www.ruby-forum.com/t/interesting-result-of-a-newbie-mistake/136925/4 "2008-05-07T13:39:23Z")

</div>

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 T.’ 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”.
