catduck If you ever played around with dynamic languages then chances are that you heard of something called Duck Typing. Yes, it is a very strange term but if you know the story behind it then the concept is very easy to digest. If you look up Duck Typing on Wikipedia you will get this definition:

Duck Typing is a style of dynamic typing in which an object's current set of methods and properties determine the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface.

The most popular phrase used to explain duck typing is..."If it walks like a duck and quacks like a duck, it must be a duck"

Still confused? Then lets clarify the concepts with a little bit of ruby code:

class Duck
def quack
puts "Quack Quack!"
end
end

class ConfusedTurkey
def quack
puts "Gobble Quack Gobble"
end
end

 

So after some observation you will notice that we have 2 distinct classes. There is no inheritance between them and they each have a quack method. So, what if I instantiated one instance of each class and put it in an array, looped over the array and called quack? ...

quackers = [Duck.new(),ConfusedTurkey.new()]
quackers.each { |i| puts i.quack() }

 

Depending on your programming background you may be surprised at the output. Why? because in a static language you would have to cast the object to the correct type before invoking it. Otherwise, you would probably get a compiler or runtime error. However, with a dynamic language your objects do not have to support a method in order for it to work. When you run the ruby code above you will get the following output:

Quack Quack!
Gobble Quack Gobble

Just to spice things up a bit, lets create a third class called Fish. To my knowledge, a Fish can not quack (lol):

class Fish  
end
 

Now, lets modify the array so we include a Fish object. Then we will call quack on all three objects:

quackers = [Duck.new(),ConfusedTurkey.new(), Fish.new()]
quackers.each { |i| puts i.quack() }


Now what happens?

Quack Quack!
Gobble Quack Gobble
undefined method `quack' for #<ConfusedTurkey:0x8f3d790>

Ok. that makes sense. The fish can not quack so we get an error. However, the point it that Ruby still attempted to make the Fish quack. It checked to see if the Fish has a quack method and reported an error when it was not found. Surprisingly enough, we can get around this problem in Ruby using a special trick, the method_missing method. Method_missing is a special method in Ruby that is invoked when a method is called on an object that does not exist. So we will modify the Fish class to look like this:

class Fish
def method_missing( sym, *args, &block )
puts "I don't know how to " << sym.to_s
end
end
 

Now when we run that same bit of code we get the following output!

Quack Quack!
Gobble Quack Gobble
I don't know how to quack

Pretty cool eh?

So, why did I decide to write an article on duck typing. Because I think that its important for people to realize that although static languages can prevent people from doing unusual things they also have the ability to slow down productivity. If I want to create an array of 50 different objects and call quack on it then let me! A warning is acceptable, but don't prevent me from running it. Perhaps I have a good reason for doing it.

At the end of the day, your job as a programmer is to determine what is the right tool for the job. Learning multiple languages is like adding new tools to your toolbox. The worst case scenario is that you will learn something new or see things in a different light. After all, learning languages that seem exotic to you will generally make you a better programmer in your language of choice.