# Re: Interesting result of a newbie mistake

**URL:** <https://www.ruby-forum.com/t/re-interesting-result-of-a-newbie-mistake/136937>\
**Category:** Ruby\
**Created:** [May 7, 2008, 3:47pm UTC](https://www.ruby-forum.com/t/re-interesting-result-of-a-newbie-mistake/136937 "2008-05-07T15:47:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 7, 2008, 3:47pm UTC](https://www.ruby-forum.com/t/re-interesting-result-of-a-newbie-mistake/136937/1 "2008-05-07T15:47:18Z")

</div>

----- Original Message ----

> From: VICTOR GOLDBERG [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Sent: Wednesday, May 7, 2008 1:10:17 PM  
> Subject: Interesting result of a newbie mistake
> 
> Instead of writing  
> a = %w{ ant cat dog }  
> I wrote  
> a = %{ ant cat dog }

That defines a string.

> puts a[2] → 110

The reason is that indexing a string in Ruby 1.8 and previous versions  
returns the character code.  
That usually comes as a surprise to beginners. This behavior changes in  
Ruby 1.9 to return the character (see  
[eigenclass.org](http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l116)).

See the following IRB Ruby 1.8 session:

$ irb

> > a = %{ ant cat dog }  
> > =\> " ant cat dog "  
> > a.class  
> > a.class  
> > =\> String  
> > a[2]  
> > a[2]  
> > =\> 110  
> > a[2].chr  
> > a[2].chr  
> > =\> “n”
> 
> I didn’t find an explanation for this result in Dave T.’ book  
> Anybody volunteers a response?

If you look for the String class and the chr method into the index of  
the first edition  
[http://ruby-doc.org/docs/ProgrammingRuby/](http://ruby-doc.org/docs/ProgrammingRuby/)  
you will find examples showing this behavior.

Christophe

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 7, 2008, 4:35pm UTC](https://www.ruby-forum.com/t/re-interesting-result-of-a-newbie-mistake/136937/2 "2008-05-07T16:35:59Z")

</div>

Thank you all that responded.  
maestroiut’s explanation was especially enlightening.

Víctor

================================================

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

----- Original Message ----

> From: VICTOR GOLDBERG [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Sent: Wednesday, May 7, 2008 1:10:17 PM  
> Subject: Interesting result of a newbie mistake
> 
> Instead of writing  
> a = %w{ ant cat dog }  
> I wrote  
> a = %{ ant cat dog }

That defines a string.

> puts a[2] → 110

The reason is that indexing a string in Ruby 1.8 and previous versions  
returns the character code.  
That usually comes as a surprise to beginners. This behavior changes in  
Ruby  
1.9 to return the character (see  
[eigenclass.org](http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l116)).

See the following IRB Ruby 1.8 session:

$ irb

> > a = %{ ant cat dog }  
> > =\> " ant cat dog "  
> > a.class  
> > a.class  
> > =\> String  
> > a[2]  
> > a[2]  
> > =\> 110  
> > a[2].chr  
> > a[2].chr  
> > =\> “n”
> 
> I didn’t find an explanation for this result in Dave T.’ book  
> Anybody volunteers a response?

If you look for the String class and the chr method into the index of  
the  
first edition  
[http://ruby-doc.org/docs/ProgrammingRuby/](http://ruby-doc.org/docs/ProgrammingRuby/)  
you will find examples showing this behavior.

Christophe
