DEV Community

Discussion on: Level Up Your Ruby Skillz: Working With Arrays

Collapse
 
swiknaba profile image
Lud

For single methods without argument, you can use shorthand syntax (aka. "passing a proc"):

[1, 2, 3, 4].count(&:even?)
# => 2
Collapse
 
codeandclay profile image
Oliver

You can also define your own methods and do something similar using &method.

def dog?(animal)
  ["spaniel", "schnauzer"].include? animal
end  
["perch", "spaniel", "haddock", "schnauzer", "cod"].count(&method(:dog?))
#=> 2