Complete the code to get the first character of the string.
word = "hello" first_char = word[[1]] puts first_char
In Ruby, string indexing starts at 0, so word[0] gives the first character.
Complete the code to get the substring "ell" from the string.
word = "hello" substring = word[[1]] puts substring
The substring "ell" starts at index 1 and ends at index 3 inclusive.
Fix the error in the code to get the last two characters of the string.
word = "world" last_two = word[[1]] puts last_two
Using negative index -2 with length 2 extracts the last two characters.
Fill both blanks to get the substring "cat" from the string.
word = "concatenate" substring = word[[1], [2]] puts substring
The substring "cat" starts at index 3 and has length 3.
Fill all three blanks to create a hash with word lengths for words longer than 3 characters.
words = ["dog", "elephant", "cat", "bird"] lengths = words.each_with_object({}) { |[3], hash| hash[[1]] = [3].[2] if [3].[2] > 3 } puts lengths
We use 'word' as key, 'word.length' as value, and iterate with 'word' over words.
