Simpleton
Simpleton

Reputation: 6415

Why is this string value a number?

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

Answers (3)

Matthew Farwell
Matthew Farwell

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

borrible
borrible

Reputation: 17346

97 is the ASCII value (in decimal) for the lowercase a.

Upvotes: 2

Jason
Jason

Reputation: 1226

97 is the ASCII decimal representation of 'a', which is the the letter located at string[1].

Upvotes: 7

Related Questions