How do I introspect things in Ruby?

前端 未结 4 943
粉色の甜心
粉色の甜心 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 18:59

    There's a module called ObjectSpace which is included into each object created in ruby. It holds all of the methods that help you introspect current context of the process. In irb you begin in Object:Main context which is top level context for current irb session. Then you could do something like time = Time.now and then do irb time which would take you into that object's context and you could inspect it from the inside without calling ObjectSpace methods on that object.

提交回复
热议问题