Why ruby is so exciting
Par bloom, jeudi 4 janvier 2007 à 17:46 :: Geekerie :: #142 :: rss
(extrait d'un slide de presentation de ruby aux développeurs Java)
Item #5 Don’t Worry About Interfaces
Ruby Uses Duck Typing
* If it walks like a duck, * And talks like a duck, o Then we can treat it like a duck. o (who cares what it really is)
class Duck
def talk() puts "Quack" end
end class DuckLikeObject
def talk() puts "Kwak" end
end flock = [
Duck.new, DuckLikeObject.new ]
flock.each do |d| d.talk end
No need to inherit from a common interface.
C'est définitivement sexy. :)
Commentaires
Aucun commentaire pour le moment.
Ajouter un commentaire
Les commentaires pour ce billet sont fermés.