Complete the code to find the first even number in the array.
numbers = [1, 3, 4, 6, 7] first_even = numbers.[1] { |n| n.even? } puts first_even
The find method returns the first element for which the block returns true.
Complete the code to detect the first word longer than 5 characters.
words = ['apple', 'banana', 'pear', 'grape'] long_word = words.[1] { |word| word.length > 5 } puts long_word
detect is an alias for find and returns the first element matching the condition.
Fix the error in the code to find the first number divisible by 3.
nums = [2, 4, 6, 9, 12] result = nums.[1] { |num| num % 3 == 0 } puts result
find returns the first element matching the condition; select returns all matching elements.
Fill both blanks to create a hash of words and their lengths, but only for words longer than 4 characters.
words = ['cat', 'elephant', 'dog', 'giraffe'] lengths = words.select { |word| word.length > 4 }.map { |word| [[1], [2]] }.to_h
The key should be the word itself, and the value should be its length using word.length.
Fill both blanks to find the first number greater than 10 and print it.
numbers = [4, 7, 12, 15, 3] first_big = numbers.[1] { |n| n [2] 10 } puts first_big
find locates the first number greater than 10 using the '>' operator. The last blank is empty because puts already prints the value.