method
detect
detect(p1 = v1)
public
Register or
log in
to add new notes.
Alex -
August 15, 2008
8 thanks
Optional Argument for detect/find [Not Documented]
detect/find’s optional argument lets you specify a proc or lambda whose return value will be the result in cases where no object in the collection matches the criteria.
classic_rock_bands = ["AC/DC", "Black Sabbath","Queen", "Ted Nugent and the Amboy Dukes","Scorpions", "Van Halen"] default_band = Proc.new {"ABBA"} classic_rock_bands.find(default_band) {|band| band > "Van Halen"} => "ABBA"
or
random_band = lambda do fallback_bands = ["Britney Spears", "Christina Aguilera", "Ashlee Simpson"] fallback_bands[rand(fallback_bands.size)] end classic_rock_bands.find(random_band) {|band| band > "Van Halen"} => "Britney Spears"