11

我想要一个单线返回真/假,它测试数组中的每个元素是否是整数。因此,如果数组中的任何元素不是整数,它应该返回 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

还有其他想法可以进一步提炼吗?

4

2 回答 2

19
array.all?{ |x| x.is_a? Integer }
于 2010-10-05T17:12:46.113 回答
4
ary.all?(&Integer.method(:===))
于 2010-10-05T17:28:29.327 回答