Enumerable Patterns
Find Detect
find and detect return the first element that matches a block condition.
Find Detect
find_detect.rb
threshold =
numbers = [3, 5, 8, 11]
found = numbers.find do |number|
number >= threshold
end
result = found || "none"
puts "threshold=#{threshold}"
puts "found=#{result}"
threshold =
numbers = [3, 5, 8, 11]
found = numbers.find do |number|
number >= threshold
end
result = found || "none"
puts "threshold=#{threshold}"
puts "found=#{result}"
threshold =
numbers = [3, 5, 8, 11]
found = numbers.find do |number|
number >= threshold
end
result = found || "none"
puts "threshold=#{threshold}"
puts "found=#{result}"
find
`find` stops once the block returns true for one element.