Get all local variables or available methods from irb?

28,799

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
28,799

Related videos on Youtube

Abhishek
Author by

Abhishek

I have been a senior developer with a focus on architecture, simplicity, and building effective teams for over ten years. As a director at Surge consulting I was involved in many operational duties and decisions and - in addition to software development duties - designed and implemented an interview processes and was involved in community building that saw it grow from 20 to about 350 developers and through an acquisition. I was then CTO setting up a dev shop at working closely with graduates of a coding bootcamp on both project work and helping them establish careers in the industry. Currently a Director of Engineering at findhelp.org your search engine for finding social services. I speak at conferences, have mentored dozens of software devs, have written popular articles, and been interviewed for a variety of podcasts and publications. I suppose that makes me an industry leader. I'm particularly interesting in companies that allow remote work and can check one or more of the following boxes: Product companies that help people in a non-trite manner (eg I'm not super interested in the next greatest way to get food delivered) Product companies that make developer or productivity tooling Funded startups that need a technical co-founder Functional programming (especially Clojure or Elixir) Companies trying to do something interesting with WebAssembly

Updated on July 09, 2022

Comments

  • Abhishek
    Abhishek almost 2 years

    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)"
    

    Is there a way to just get a list of what local variables or methods ARE available? This would be really useful for exploring ruby.

  • Zabba
    Zabba about 13 years
    @sawa, inspect will show instance_variables too (i.e. unless it has been overridden for a class to show something else instead)
  • lukemh
    lukemh almost 13 years
    can someone explain how this guy gets the prompt: "show all available 152) methods?" youtube.com/watch?v=J_9H1WPV2Ws#t=0m15s
  • Zabba
    Zabba almost 13 years
    @lukemh, type [1,2]. into irb and press TAB twice. The feature in general is called completion / auto-completion.
  • RoUS
    RoUS almost 9 years
    You should probably change that to my_object.methods.sort - Object.new.methods -- as it stands, you're subtracting Object's class methods from the list of my_object's instance methods.
  • Swanand
    Swanand almost 9 years
    True. However in Ruby, Object.class is Class which is an instance of the class Class, so it still has the methods from Object.methods.