我想要一个单行返回true/false,它测试数组中的每个元素是否为整数.因此,如果数组中的任何元素不是Integer,则应返回false,否则返回true.这是我的尝试:
>> ([2,1,4].map {|x| (x.is_a? Integer)}).reduce {|x, result| x and result}
=> true
>> ([2,"a",4].map {|x| (x.is_a? Integer)}).reduce {|x, result| x and result}
=> false
Run Code Online (Sandbox Code Playgroud)
还有其他想法可以进一步提炼它吗?
ary.all?(&Integer.method(:===))
Run Code Online (Sandbox Code Playgroud)