You cannot, because && and || , unlike other operators, are not syntactic sugar for methods (i.e. there is no method called && or || ), so you cannot refer to them using a symbol.
However, you can avoid using inject to compute a logical conjunction or disjunction of an array of booleans by replacing it with all? or any? accordingly, because for any array the following conditions are true:
ary.inject(true) { |res, b| res && b } == ary.all? ary.inject(false) { |res, b| res || b } == ary.any?
So, for example, the code you posted can be rewritten as:
[2,4,6].map(&:even?).all? # => true
Update : it is obvious that my last example is the wrong way to express this calculation, falsetru's answer is much faster:
require 'fruity' compare( -> { (0..1000).map(&:even?).all? }, -> { (0..1000).all?(&:even?) } )
Running each test 1024 times. Test will take about 2 seconds. Code 2 is faster than Code 1 by 111x ยฑ 10.0
toro2k
source share