Ruby hook for a specific method?

I searched this for a search and could not find an answer that makes me think that the answer is no, but I decided that I would ask here if someone knows for sure.

Does Ruby have a hook when methods are defined (i.e. on a module or class)?

If not, is anyone familiar enough with the implementation of an object mainto find out exactly how it copies methods to Objectwhen they are defined at the top level?

Really curious about that. Thanks for any info :)

+6
source share
2 answers

This is true. Module#method_added https://ruby-doc.org/core-2.2.2/Module.html#method-i-method_added

module Thing
  def self.method_added(method_name)
    puts "Thing added #{method_name}"
  end
  def self.a_class_method; end
  def do_something; end
end

class Person
  def self.method_added(method_name)
    puts "I added #{method_name}"
  end
  attr_accessor :name 
end

Thing
Person.new 

# Thing added do_something
# I added name
# I added name=
+6
source

If not, is anyone familiar enough with the implementation of an object mainto find out exactly how it copies methods to Objectwhen they are defined at the top level?

" ". , , , Object. , , , , class Foo, class Foo. , . main .

, default definee, , , definee ( def foo.bar; end). self , Object. , instance_eval class_eval .., .

0

All Articles