在Ruby中键入数组索引值的检查

Wil*_*iam 0 ruby

在Javascript中,如果我想对数组索引进行类型检查,我可以这样做:

var array = [1,2,3,4,"the monster from the green lagoon"]

for (i=0;  i < array.length;  i++) {

    if (typeof(array[i]) === 'number')      {
    console.log("yes these are all numbers");

    }

    else        {
    console.log("Index number " + i + " is " +  array[i] +": No this is not a number");

    }

}
Run Code Online (Sandbox Code Playgroud)

在Ruby中,我不明白如何做到这一点.我正在尝试对整数进行检查.我知道在Ruby世界中,使用每种方法被认为是很好的礼仪,因此基本的循环是这样的:

array = [1, 2, 3, 4, 5, 6]
array.each { |x| puts x }
Run Code Online (Sandbox Code Playgroud)

我很困惑的部分是语法是外来的,我不清楚逻辑在哪里.我还没有进行实际的类型检查,但是根据我的阅读,它将与Integer类型进行比较:

if array[i] == Integer
Run Code Online (Sandbox Code Playgroud)

谢谢.

hal*_*elf 8

a = [1,2,3,4,5]
a.all? { |x| x.is_a? Integer } 
Run Code Online (Sandbox Code Playgroud)