Array.try_convert
可以通过列出类方法Objects#methods
Array.methods
#=> [:try_convert, :[], :allocate, :superclass, :new, :<=>, :<=, :>=, :==, ...]
Run Code Online (Sandbox Code Playgroud)
它返回许多其他方法,因为(Array
在中Class
)还包含来自的实例方法Class
。要只获取Array
特定的类方法,我们可以传递false
:
Array.methods(false)
#=> [:try_convert, :[]]
Run Code Online (Sandbox Code Playgroud)
Array#at
可以通过Module#instance_methods
以下方式列出实例方法:
Array.instance_methods
#=> [:to_h, :include?, :at, :fetch, :last, ..., :instance_eval, :__id__, :__send__]
Run Code Online (Sandbox Code Playgroud)
同样,我们可以通过false
以排除继承的方法:
Array.instance_methods(false)
#=> [:to_h, :include?, :at, :fetch, :last, ..., :slice, :slice!, :dig, :hash]
Run Code Online (Sandbox Code Playgroud)