Ruby Duck Typing

In Ruby duck typing means that it does not rely on the type of class. It’s more concerned about what the class can do. The object is only defined by what it can do and is not limited by a specific type. In Ruby if you need to find out if the object implements a specific method you can use the respond_to? method. The reason why it’s called duck typing is a little saying in Ruby if it walks like a duck and talks like a duck then it’s a duck. That is where the saying came from.

In contrast Java does not use duck typing. You have to declare specific types within methods and classes. Also you have to declare the return type within the method definition. So it’s not as flexible as Ruby. Therefore it’s not considered to have the ability of duck typing.

Leave a Reply

Your email address will not be published. Required fields are marked *