Because :any?(&:zero?)
is not an appropriate symbol literal. You can make it a symbol by doing :"any?(&:zero?)"
, but still, there is no such method.
Chaining block passing in ruby [duplicate]
Pergunta
lets say I have an array
array = [[2], [1], [0]]
I can do this:
array.map(&:any?)
# => [true, true, true]
and I can do this:
array.map do |x|
x.any?(&:zero?)
end
# => [false, false, true]
why cant i do this? (or something similar):
array.map(&:any?(&:zero?))
returns SyntaxError: unexpected '(', expecting ')'
array.map(&:any?(&:zero?))
Thank you for your time.
Solução
Outras dicas
Reasons why are explained in other answers. You can however use the shortcut for anything if you define a proc:
any_zeros= ->(o) { o.any?(&:zero?) }
array = [[2], [1], [0]].map(&any_zeros)
Why? Simple answer: because Ruby doesn't include syntax for it.
You are basically trying to pass a default argument to the method. The Ruby devs, at some point, made a decision that a feature like this was too much work and extra complication for what can be achieved using this little code:
array.map {|e| x.any?(&:zero?)}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow