Extending Object in Ruby
Ruby allows us to extend objects with module methods without including modules into Classes. Ruby uses “extend” method to accomplish this. Here is an example that shows you the way to accomplish this.
class MyClass
def my_name
@name = “John”
end
end
module MyModule
def extended_method
@surname = “Tomphson”
end
end
person = MyClass.new?
person.extend(MyModule)
person.extended_method //Object has module methods without class inclusion.
This approach provides developer with additional flexibility while working with Ruby modules.