Ruby On Rails, Design, Simplicity, Web 2.0, Ajax, Mac and Tons of Pizza.

Mar 04

Ruby Pocket Session #2 - Duck typing

Posted by Massimo Sgrelli in Ruby on Rails - no comments digg this add to delicious

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 type 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 pass 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 don’t think 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 feature. 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 concept. Thanks Ruby. Thanks Dave

(Ref. See Ruby Pocket Reference p. 11)

Post a comment

Categories:

Tags:

Powered by Mephisto, Valid XHTML 1.1, Valid CSS - Supported by Wave Factory