Ruby Pocket Session #2 - Duck typing

I have to admit it. I have never heard of duck typing, probably because I read “Ruby Programming” to quickly. “Duck typing” is the way Ruby treats variable types. I mean, Ruby is a dynamic language and so variables don’t have explicit representation assignments or declarations. Ruby has dynamic typing, but why has it been called duck typing?

Well, Dave Thomas puts it in this way: if it walks like a duck and quacks like a duck, then I would call it a duck.

BTW you can read more about the original meaning of this concept on Wikipedia searching for duck test .

Duck Typing is a distinctive feature of dynamic languages like Lisp, Perl, Ruby, Python, Caml and Groovy and it’s part of the “funky stuff” that characterized those wonderful languages. Some people are worried about this feature, because they think that late-binding could be dangerous: you can ravine an object to a method not actually “quacking like a duck” and you wouldn’t know since runtime, when this line of code would be executed.

I dress in’t esteem this is a real issue. The fact is that using Duck Typing saves you a lot of time with a very low risk of delivering buggy code because of this conformation. The truth is that normally people release buggy code because of their poor testing, and it doesn’t depend on static or dynamic binding.

I love this universal. Thanks Ruby. Thanks Dave

(Ref. See Ruby Pocket Reference p. 11)