ktulasi
Joined: 21 Oct 2009 Posts: 55
|
Posted: Wed Oct 21, 2009 12:37 pm Post subject: Using Mixins With Rails |
|
|
hi,
Mixins are one of the most powerful constructs that Ruby provides. Mixins give you the power of multiple inheritance without all the inherent complexities and problems associated with multiple inheritance. Ruby’s opted to not allow multiple inheritance, but to allow for code from one module to be able to be included in another class.The best part of using mixins is how extremely simple they are to use. Just include the module you want to mixin and then call any of the functions defined in the module.
So consider the following code:
module Example
def hello_world
puts "Hello World"
end
end
class BaseClass
def class_method
puts "In class method"
end
end
class Foo < BaseClass
include Example
end
f = Foo.new
f.class_method
f.hello_world
In this example, we are creating a module named Example with a hello_world method, a class named BaseClass, and then another class Foo that extends BaseClass and includes Example. The class Foo will then have both the methods from Example and BaseClass,
So that is a basic example of what a mixin is.
thanq  |
|