Get all local variables or available methods from irb?

前端 未结 3 982
深忆病人
深忆病人 2021-02-12 09:36

When I go into irb and type in a command that does not exist I get an error stating

\"undefined local variable or method \'my_method\' for main:Object (NameError         


        
3条回答
  •  深忆病人
    2021-02-12 10:12

    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

提交回复
热议问题