[Features] Polymorphic

    Ruby has many features and attributes which are familiar in the world of programming. One of the most outstanding benefits of programming in Ruby is the fact that it is a polymorphic language by nature, and one can easily develop his applications with polymorphism in mind. 

    To define polymorphism in Ruby terms, it is the ability to call a method on multiple types of Objects. We have shown this feature without explaining it, however it can be simply put by example. So lets take a look at it in action. 

#Declaring some variables.
>> string1 = "ruby"       
=> "ruby"
>> string2 = "is"
=> "is"
>> string3 = "polymorphic"
=> "polymorphic"
>> number1 = 1
=> 1
>> number2 = 2
=> 2
>> number3 = 3
=> 3

#Performing the + function on a number type.
>> sum1 = number1 + number2
=> 3

#Performing the + function on a string type.
>> string_concat = string1 + string2 + string3 
=> "rubyispolymorphic"

    The + method when dealing with a number type, performs a mathematical addition operation. When the + method is used on a string type, it concatenates or joins the strings. Lets look at dealing with arrays (of strings)

 #Creating some variables to hold arrays.
 >> array1 = %w{ruby is a}
=> ["ruby", "is", "a"]
>> array2 = %w{feature rich}
=> ["feature", "rich"]
>> array3 = %w{fun language}
=> ["fun", "language"]

#Performing the + method the arrays
>> array1 + array2 + array3
=> ["ruby", "is", "a", "feature", "rich", "fun", "language"]

#Using the multiplication method (*), on numbers.
>> result = 5 * 8
=> 40
>> puts result
40

#Using the multiplication method on strings.
>> result2 = "string " * 3
=> "string string string "

#Using the multiplication method on an array
>> array_new = %w{some words together in an array} * 3
=> ["some", "words", "together", "in", "an", "array", "some", "words", "together", "in", "an", "array", "some", "words", "together", "in", "an", "array"]

Of course these are simple examples of polymorphism. Basically whats happening here is hidden behind the scenes, however think of it as the difference between Fixnum.* and String.* where these are two separate classes which BOTH have a method *. The definition of each method is different. Every method does NOT work like this, you can not divide strings or perform complex math operation on them, because they are not numbers. 

    Lets create our own Polymorphism with Animals for fun.

#Creating an empty animal class. 

class Animal
end

#Creating the Dog which inherits from the Animal Class
class Dog < Animal

    def initialize(name,age,type)
        @name = name
        @age = age
        @type = type
        if defined?(@@count)
            @@count += 1
        else
            @@count = 1
        end
    end

    def make_native_sound
        puts "Bark Bark Bark, I am #{@name} the #{@type} dog, I am #{@age} and I am hunggggaarry!"
    end

    def self.count
        @@count
     end
end

class Cat < Animal

    def initialize(name,age,type)
        @name = name
        @age = age
        @type = type
        if defined?(@@count)
            @@count += 1
        else
            @@count = 1
        end
    end

    def make_native_sound
        puts "Meow, Meow, I'm #{@name} the #{@type} cat, I am #{@age} and I want cat food! OK Im going to scratch the Sofa, bye bye now!!"
    end

    def self.count
        @@count
     end
end


#Creating some variables which reference our Cat and Dog objects.
>> queenie = Cat.new("Queenie Simmons", 1, "Domestic")
=> #<Cat:0x7f7aaadb77f8 @name="Queenie Simmons", @age=1, @type="Domestic">
>> fido = Dog.new("Fido Simmons", 2, "Golden Retriever")
=> #<Dog:0x7f7aaad92570 @name="Fido Simmons", @age=2, @type="Golden Retriever">

#Calling the method make_native_sound on each animal.
>> queenie.make_native_sound
Meow, Meow, I'm Queenie Simmons the Domestic cat, I am 1 and I want cat food! OK Im going to scratch the Sofa, bye bye now!!
=> nil
>> fido.make_native_sound
Bark Bark Bark, I am Fido Simmons the Golden Retriever dog, I am 2 and I am hunggggaarry!
=> nil

    This is a rather simple example...








Comments