Reputation: 1636
As I understand ruby classes, they are almost the same as modules except with an added functionality of being able to instantiate it. As Class
inherits from Module
, I assumed that then every class (objects of class Class
) would have access to module_function
, but it does not seem to be the case. When I did a difference of Module and Class' private_instance_methods, I found that Module
has 3 methods more than Class
- [:append_features, :extend_object, :module_function]
How were these functions removed from the call chain for Class objects and more importantly why?
Upvotes: 2
Views: 544
Reputation: 168269
Those core features are implemented in C, so discussing about that does not have generality, and is not useful. Within Ruby, you can undefine an inherited method without undefining the method in the superclass by using undef
.
class Foo
def foo; end
end
class Bar < Foo
undef :foo
end
Foo.new.foo
# => nil
Bar.new.foo
# => NoMethodError: undefined method `foo' for #<Bar:0x007f85c3ce3330>
append_features
is a hook to be called right before a module is include
-d, which a module can be, but not a class.extend_object
is a hook to be called right before a module is extend
-ed, which a module can be, but not a class.module_function
is to double the method as a class method and a private instance method, latter of which is useful when you include that module, which can be done with a module but not with a class.Upvotes: 3