How do I introspect things in Ruby?

前端 未结 4 891
粉色の甜心
粉色の甜心 2021-01-31 18:57

For instance, in Python, I can do things like this if I want to get all attributes on an object:

>>> import sys
>>> dir(sys)
[\'__displayhook__         


        
4条回答
  •  故里飘歌
    2021-01-31 19:20

    If you have an object, and you want to know what methods it responds to, you can run obj.methods (and all of the tricks that thenduks has mentioned on this result.)

    If you have a class, you can run klass.methods to see what class methods are availabe, or you can run klass.instance_methods to know what methods are available on instances of that class. klass.instance_methods(false) is useful, becuase it tells you what methods were defined by the class and not inherited.

    There's now way to get help text for a method within Ruby the way python does.

提交回复
热议问题