what is wrong? `print([1, 3, 6, 9 ].find { |e| e < 9 })` in ruby

The error in your code is that you're using the .find method incorrectly. The syntax you're using is from Ruby's Enumerable module, but you're trying to call it on an array directly.

To fix this, you can use the .index method instead to find the index of the element that matches the given condition. Here's the corrected code:

main.rb
puts [1, 3, 6, 9].index { |e| e < 9 }
38 chars
2 lines

This will output 2 because the element 6 (at index 2) is the first element that satisfies the condition e < 9.

Alternatively, if you want to find the element itself instead of its index, you can use the .find method as follows:

main.rb
puts [1, 3, 6, 9].find { |e| e < 9 }
37 chars
2 lines

This will output 6, the first element that satisfies the condition.

Remember to use puts to print the result to the console.

related categories

gistlibby LogSnag