Ruby Programming Tutorial - Lesson 26 - Mixing - Example of Animal

in ruby •  7 years ago  (edited)

2017_11_12_85_0cb95ccbac6441e7a30fb7d1d01b094d.bc1ee.png

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

Screenshot from 2018-03-18 04-43-41.png

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.

Screenshot from 2018-03-18 04-45-06.png

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..

Screenshot from 2018-03-18 04-53-52.png

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 ..

Screenshot from 2018-03-18 04-57-14.png

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
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

please like my picture

well, a little tecnic, but well done.

Thank you :)

Learnt ruby at a point in time on my mobile phone using solo learn . pls I need a good program for PC for ruby.

Thank you for your greate info, guys.

Nice work

Thanks for following me. I have returned the favor.

Intresting job. Congratulations