Reputation: 6415
In the Ruby Koans, you have to fill in the blank for what represents string[1]
below. Why is the answer 97?
def test_you_can_get_a_single_character_from_a_string
string = "Bacon, lettuce and tomato"
assert_equal ___ , string[1]
end
Upvotes: 3
Views: 467
Reputation: 61705
string[1]
will give you the second character of string, the first being 0. This is converted to an integer, 'a' being represented as 97 in ASCII. So this gives you 97.
Upvotes: 2
Reputation: 1226
97 is the ASCII decimal representation of 'a', which is the the letter located at string[1].
Upvotes: 7