在Ruby中,如果global_variables.class返回Array,您如何判断global_variables数组还是方法?
挖这个:
>> 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)