How to capture greps return value in an if statement for use inside block ?
colors = ["red", "blue", "white"]
if color = colors.grep(/^b/) # Would be nice to capture the color blue
puts "Found #{color.first}." # with the regex, and pass it down to block
else
puts "Did not find the first color."
end
How could we express this differently?
You could do something like this:
to capture the array and check if it is empty in one go. If you only want
found.firstthen I’d go with Jakub’s.