Let’s say I have this method num_i_want?
, and I want to use it with select
to filter my nums
array.
def num_i_want?(num)
num % 2 == 0
end
nums = [1, 2, 3, 4]
I try to use the method as a block using the unary ampersand operator:
nums.select( &num_i_want? )
But I get this error:
ArgumentError: wrong number of arguments (0 for 1)
from (irb):1:in `num_i_want?'
from (irb):6
Why am I getting this error? And what is the simplest code I can use instead?
I know that &:num_i_want?
doesn’t work. That tries to call num_i_want?
on each number, which raises NoMethodError
because Integer#num_i_want?
doesn’t exist.
Of course, I could use &:even?
to call Integer#even?
, but let’s pretend the implementation of num_i_want?
is more complicated than that.