In Ruby we have simple ways to get all the local variables, global variables with local_variables
and global_variables
method.
We can list the constants with Object.constants
But is there a builtin way to list all the Object methods?
Something like this:
def foo() end
def bar() end
def baz() end
# As `Array.new.methods' or `Array.instance_methods` returns all the methods of an Array object...
# Code to return all the methods defined above # => [:foo, :bar, :baz]
In IRB, I can write:
def foo() end
p [self.methods.include?(:foo), self.respond_to?(:foo)]
And the output is [true, true]
in IRB, but in a file, the output to standard output is [false, false]
Similarly if I run the following code:
def foo() end
puts Object.new.methods.include?(:foo)
In IRB, I get true
, and if it's saved in a file, I get false
Here's a link which didn't help much:
How to list all methods for an object in Ruby?
Just because it talks about getting methods of a class or a module. But I want to list the methods defined in the top self object.