Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Foundations Strings Working With Strings

Alexander Bromage
PLUS
Alexander Bromage
Courses Plus Student 4,014 Points

Why did the ASCII value for the letter not appear when I used first_name[0]? Instead I got the actual letter "A".

In the video Jason sees the ASCII value and not the letter then refers to the ASCII tables to look it up. As I follow along with the exact same code (checked several times) the ASCII value does not appear the letter does.

3 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

I was curious, because what you are saying was also true in my case. So I player a bit with rvm, installed various versions of Ruby and it seems that the behavior from the videos is true for Ruby 1.8.7 and earlier. Everything after (1.9.x, 2.x) this returns letters, not ASCII codes.

Alexander Bromage
Alexander Bromage
Courses Plus Student 4,014 Points

But in the video he installs a 1.9.x version doesn't he? Well it isn't the most important aspect but just interested to understand why. Thanks for responding.

To figure out the Ordinal code you can use the following method .ord

puts "A".ord
# returns 65 for the Capital A
puts "a".ord
# returns 97

In Ruby 1.8.7 it used the question mark ? to find out the ordinal value, but it changed since Ruby 1.9. A lot of things changed between versions.

I also get A when I run

first_name = "Aurel"
first_name[0]
=> "A"

As if the string is an array of characters.

irb(main):023:0> first_name[0]
=> "M"

irb(main):025:0> first_name[0].ord
=> 77