scores with the exact numbers: 3, 7, 8, 10, 15is_even that holds a lambda to check if a number is evenfind method with is_even to get the first even number from scoresputsscores with the exact numbers: 3, 7, 8, 10, 15is_even that holds a lambda to check if a number is evenfind method with is_even to get the first even number from scoresputsscores with these exact numbers: 3, 7, 8, 10, 15Use square brackets [] to create an array and separate numbers with commas.
is_even that holds a lambda which returns true if a number is evenUse ->(num) { num.even? } to create a lambda that checks if num is even.
find method on scores with the is_even lambda to get the first even number and store it in first_evenUse scores.find(&is_even) to find the first number where is_even returns true.
first_even using puts to show the first even number foundUse puts first_even to print the value.
What does the Ruby method find (also known as detect) do when called on an array?
findfind method searches through the array elements one by one.find returnsfind = first matching element [OK]Which of the following is the correct syntax to find the first even number in an array nums using find?
nums = [1, 3, 4, 6]
find{ |n| n.even? }. Others have syntax errors.What is the output of the following Ruby code?
words = ["apple", "banana", "cherry", "date"]
result = words.find { |w| w.length > 5 }
puts resultIdentify the error in this Ruby code that tries to find the first negative number:
numbers = [3, 5, -2, 7] result = numbers.find do |n| n < 0 end puts result
do...end block with variable n is valid syntax for find.find accepts a block and returns the first element matching n < 0.You have an array of hashes representing people:
people = [
{name: "Alice", age: 18},
{name: "Bob", age: 17},
{name: "Carol", age: 22}
]Which code finds the first person who is an adult (age 18 or older) and returns their name?
p[:age] >= 18.find or detect with condition, then access [:name].Pass the test and this pattern moves unknown