如何在if語句中捕獲greps返回值以便在塊內使用?ruby - 爲greps返回值賦值if語句
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
我們如何表達這種不同?
我接受了您的提示,並意識到這有點無意義:)現在是時候讓我猜測了。 – 2012-03-23 03:01:42
@NiklasB .:不一定毫無意義,有時很好玩。 – 2012-03-23 03:17:06