codedecoder

breaking into the unknown…

polymorphism in ruby: duck typing with example

Leave a comment

If you interested in technical jargon about polymorphism read here . Keeping it simple, polymorphism is object oriented programing approach where a interface behave differently depending on nature of the object.

what ? .. you still feel it is technical jargon.

O.K…lets make it more simple. being human we tend to meet people, greet them, treat them. Have you observed that people from different country, state or city do it in different way. A Englishman greet you with “Hi..How are you?” , a Indian from north say “namaste..kaise hain aap” …and so on.

So if you, program a welcome interface, its behavior should change depending on the person. This is known as Polymorphism.If you still didn’t get it, continue with the example, by end your concept will be clear.

class Welcome

  def meets
    puts "meets a person"
  end

  def greet(person)
    person.greet
  end

  def treat(person)
    person.treat
  end

end

class Englishman

  def greet
    puts "Hi..how are you?"
  end

  def treat
    puts "Iam serving you English food"
  end

end


class Indian

  def greet
    puts "Namste...kaise hain app"
  end

  def treat
    puts "Iam serving you Indian food"
  end

end

welcome = Welcome.new
puts "********Englishman***********"
person = Englishman.new
welcome.greet(person)
welcome.treat(person)


puts "********Indian***********"
person = Indian.new
welcome.greet(person)
welcome.treat(person)

If you run the above file on console, you will again get the same output as in template implementation of inheritance.

$ ruby welcome.rb
********Englishman***********
Hi..how are you?
Iam serving you English food
********Indian***********
Namste…kaise hain app
Iam serving you Indian food

 

You can see that the welcome interface’s greet and treat method behaving differently for different person. This is known as polymorphism in Object oriented programing. The implementation is known as Duck Typing . The name come from initial proposed theory that a bird can speak and fly, but can do it in any ways, but still be treated as bird. They can be a duck or penguin or crow and so on

You can demo it on same line as welcome interface above. create a bird.rb file and add below code to it.

class Duck
  def speak
    puts "Quack I am #{@name}"
  end
 
  def fly
    puts 'Up up and away...'
  end
end
 
class Penguin
  def speak
    puts "Squak I am #{@name}"
  end
 
  def fly
    puts 'Nope. I swim...'
  end
end

class Bird
  def speak(bird)
    bird.speak
  end
 
  def fly(bird)
    bird.fly
  end
end

bird = Bird.new

puts "***behave as duck****"
duck = Duck.new
bird.speak(duck)
bird.fly(duck)

puts "***behave as penguin****"
penguin = Penguin.new
bird.speak(penguin)
bird.fly(penguin)

If you run the above file you will get below output:

$ ruby bird.rb
***behave as duck****
Quack I am
Up up and away…
***behave as penguine****
Squak I am
Nope. I swim…

Hope now you know what is Duck Typing and what is Polymorphism.

 

Reference:

https://practicingruby.com/articles/duck-typing-in-practice-2

Advertisements

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s