In this article we are going to take an example of a Mixin
we are going to mix following classes with a single module, creating MIXINs and using them in our ruby program.
We have created a module that has few general/common properties of animals
module Animalproperties
LEGS = 4
EYES = 2
EARS = 2
def walk
puts "I can Walk"
end
def speak
puts "I can Speak"
end
def show_properties
puts "I have #{LEGS} legs"
puts "I have #{EYES} eyes"
puts "I have #{EARS} ears"
end
end
We have called this module Animalproperties ..
It has few methods that we want to use for cats, dogs, and few other animals.
After creating a module, lets now Create few MIXINs where we want to use this module.
lets create a MIXIN for cats and another MIXIN for dogs.
This is dog MIXIN which is used to create dog objects.
require_relative 'Animal'
class Dog
include Animalproperties
def initialize(color)
@dog_color = color
end
def speak
puts "Wooo Waoooo!! "
end
end
Here is a cat MIXIN , which is used to create Cat objects,
require_relative 'Animal'
class Cat
include Animalproperties
def initialize(color)
@cat_color = color
end
def speak
puts "Meowww Meoww !! "
end
end
When we added module in our classes, we made that module's methods to be available for our MIXIN to take benefit of.
in above example we see that, our module, and the MIXIN have a method that exists in both, the module and the MIXIN ..
def speak
This method exists in both module and the MIXINS ..
Which of the methods should get called ? when we create a new object from lets say Cat MIXIN .
my_cat = Cat.new("Brown")
When ruby finds a method in MIXIN it calls it, and neglects the method in module ... but if method does not exists in MIXIN, it then calls the method of module.
That's why I have created a third mute animal.. which does not have a sound of its own..
finally we add our cats, dogs, and mute animals in our main.rb where we create objects from them
and use them in our program..
You should be able to grasp the MIXINs now .. in future we will just call them Classes ..
Here is the code,
You can copy it, and play with it. :) I am sure you can create beautiful MIXINs now :) and use them in your programs.
require_relative 'cat'
require_relative 'dog'
require_relative 'muteanimal'
_mycat = Cat.new("Black")
_mycat.speak
_mycat.walk
_mycat.show_properties
_mydog = Dog.new("White")
_mydog.speak
_mydog.walk
_mydog.show_properties
_muteAnimal = Mute.new("Red")
_muteAnimal.speak
_muteAnimal.walk
_muteAnimal.show_properties
please like my picture
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
well, a little tecnic, but well done.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Learnt ruby at a point in time on my mobile phone using solo learn . pls I need a good program for PC for ruby.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you for your greate info, guys.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Nice work
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks for following me. I have returned the favor.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Intresting job. Congratulations
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit