Can anyone elaborate on the differences between ruby's Object#define_method
and Module#define_method
and where they are generally used?
Object#define_method
does not exist:
o = Object.new
o.define_method
#NoMethodError: undefined method `define_method' for #<Object:0x1448a80>
However, Object.define_method
exists:
Object.define_method
#NoMethodError: private method `define_method' called for Object:Class
That's because Object
is an object of class Class
, and Class
is a subclass of Module
:
Object.class # => Class
Class.ancestors # => [Class, Module, Object, Kernel, BasicObject]
So when you call Object.define_method
, you're calling Module#define_method
.
Just remember that classes are objects of class Class
, and it'll be as clear as mud!
Object#define_method
is actually Module#define_method
.
pry(main)> Object.method(:define_method).owner
=> Module
pry(main)> Module.method(:define_method).owner
=> Module
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With