Ruby - determining method origins?
Object#method
returns a Method
object giving meta-data about a given method. For example:
> [].method(:length).inspect
=> "#<Method: Array#length>"
> [].method(:max).inspect
=> "#<Method: Array(Enumerable)#max>"
In Ruby 1.8.7 and later, you can use Method#owner
to determine the class or module that defined the method.
To get a list of all the methods with the name of the class or module where they are defined you could do something like the following:
obj.methods.collect {|m| "#{m} defined by #{obj.method(m).owner}"}
Determine the class to which a method belongs in rails
Ruby is an object-oriented language. And while methods aren't objects in Ruby, you can ask Ruby to give you a Method
object representing the method in question, and then you can simply tell that Method
to give you its owner:
Ap::Application.routes.draw do
p method(:resources).owner
end
What is origin variable in Exception Object/Class of Ruby?
Exception does not have any such method, nor do any core classes or classes in the standard library (as determined by running "ri origin" with Ruby 1.9.2).
Get source from where function was called from / get last function from backtrace
caller_locations(1,1)[0].label
If you extract this into a method, you might need to modify the first argument of caller_locations to look further up the stack trace.
RubyMonk Tracking Methods
I finally figured it out, I walked away from it yesterday till this morning. I guess fresh eyes helped.
class Dojo
@@singleton_methods_added = []
def self.singleton_methods_added
@@singleton_methods_added
end
def self.singleton_method_added(method_name)
@@singleton_methods_added << method_name
end
end
Ruby: Not able to understand why class method is accessible in child class
Your understanding is not entirely correct. object.class.ancestors
does not give you the full list of classes/modules where methods will be searched for. Here is a counter example:
foo = Object.new
def foo.bar
puts "I'm inside the singleton class"
end
foo.class.ancestors # => [Object, Kernel, BasicObject]
foo.bar # "I'm inside the singleton class"
Instead you have to start with the singleton class of the object:
foo.singleton_class.ancestors # => [#<Class:#<Object:0x007fa92c33c610>>, Object, Kernel, BasicObject]
foo.singleton_class.instance_methods.include?(:bar) # => true
Knowing that when invoking object.method_name
, #method_name
is searched for somewhere in
object.singleton_class.ancestors
and that Ruby classes are regular objects, it only stands that with Child.parent
, #parent
will be looked for somewhere in
Child.singleton_class.ancestors
The magic is that class methods in Ruby aren't special in any way. They are just defined in the singleton class of the class. In your example:
Child.singleton_class.ancestors # => [#<Class:Child>, #<Class:Parent>, #<Class:Object>, #<Class:BasicObject>, Class, Module, Object, Kernel, BasicObject]
As you can see, the Child
's singleton class ancestors chain includes the Parent
's singleton class. That is why when you invoke Child.parent
you actually call Parent.parent
.
Related Topics
How to Return Something Early from a Block
Rails. How to Store Time of Day (For Schedule)
Preferred Ruby Plugin for Eclipse
Rspec: How to Stub an Instance Method Called by Constructor
Is Inject the Same Thing as Reduce in Ruby
How to Use Redis in a Multi-Threaded Rails Environment? (Puma/Sidekiq)
How to Use Correct Ruby in Vim? How to Modify $Path in Vim
Error While Trying to Load the Gem 'Devise. Activesupport: Duration Can't Be Coerced into Integer
Migrating from Rails 2 to Rails 3
Cannot Execute "Rails Console" Due to an Error with Readline
Where in the Ruby Language Is %Q, %W, etc., Defined
Rails 4 How to Ignore Pending Migrations
How to Get the Line of Code That Triggers a Query
What Does the Fail Keyword Do in Ruby