开发者

Ruby - test each array element, get one result

I want a one-liner to return true/false, that tests each element in an array fo开发者_高级运维r whether it's an Integer or not. So if any element in the array is not an Integer, it should return false, else true. Here's my try:

>> ([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

Any other ideas to distill it down further?


array.all?{ |x| x.is_a? Integer }


ary.all?(&Integer.method(:===))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜