Get all local variables or available methods from irb?

£可爱£侵袭症+ 提交于 2019-12-03 09:43:43
Zabba

Look for methods in the Kernel, Object and Module : e.g. local_variables, instance_methods, instance_variables.

Other great methods in there. inspect is another one.

Great answers.
As you explore, you have these at your disposal:

obj.private_methods 
obj.public_methods 
obj.protected_methods 
obj.singleton_methods

and

MyClass.private_instance_methods 
MyClass.protected_instance_methods 
MyClass.public_instance_methods

Usage like :

obj.public_methods.sort

Can make review easier too.

Some special cases exist like

String.instance_methods(false).sort

... will give you only the instance methods defined in the String class, omitting the classes it inherited from any ancestors. As I expect you know, you can see more here: http://www.ruby-doc.org/docs/ProgrammingRuby/ but it's not as fun as inspecting and reflecting in irb.

Happy exploring -

Perry

To find out instance variables, you can use Kernel#instance_variables as Zabba pointed out.

For methods available on an object, I use my_object.methods - Object.methods to find out what non-obvious methods are available to my object. This narrows down the list and is considerably easy to read.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!