在Ruby中,如果"global_variables.class"返回"Array",那么如何判断global_variables是数组还是方法?

nop*_*ole 4 ruby types class

在Ruby中,如果global_variables.class返回Array,您如何判断global_variables数组还是方法?

Eim*_*tas 7

挖这个:

>> global_variables
=> ["$-l", "$LOADED_FEATURES", "$?", ... , "$SAFE", "$!"]
>> method(:global_variables)
=> #<Method: Object(Kernel)#global_variables>
Run Code Online (Sandbox Code Playgroud)

为了比较:

>> method(:foo)
NameError: undefined method `foo' for class `Object'
    from (irb):6:in `method'
    from (irb):6
>> 
Run Code Online (Sandbox Code Playgroud)