From ruby-talk-admin@ruby-lang.org Fri Dec 16 02:32:18 2005 Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (8.12.3/8.12.3/Debian-6.6) with ESMTP id jBFHWHZk029623; Fri, 16 Dec 2005 02:32:17 +0900 Received: from funfun.nagaokaut.ac.jp (funfun.nagaokaut.ac.jp [133.44.2.201]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 553335A68; Fri, 16 Dec 2005 02:32:22 +0900 (JST) Received: from localhost (localhost.nagaokaut.ac.jp [127.0.0.1]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id 5CFACF04842; Fri, 16 Dec 2005 02:32:22 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id 0E734F04866; Fri, 16 Dec 2005 02:32:21 +0900 (JST) Received: from beryllium.ruby-lang.org (beryllium.ruby-lang.org [210.163.138.100]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id 0FAA4630024; Fri, 16 Dec 2005 02:32:21 +0900 (JST) Received: from beryllium.ruby-lang.org (beryllium.ruby-lang.org [127.0.0.1]) by beryllium.ruby-lang.org (Postfix) with ESMTP id 889B733BE8; Fri, 16 Dec 2005 02:32:20 +0900 (JST) Received: from localhost (beryllium.ruby-lang.org [127.0.0.1]) by beryllium.ruby-lang.org (Postfix) with ESMTP id 66FFC33DC2 for ; Fri, 16 Dec 2005 02:32:13 +0900 (JST) Received: from beryllium.ruby-lang.org ([127.0.0.1]) by localhost (beryllium.ruby-lang.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 12851-04 for ; Fri, 16 Dec 2005 02:32:13 +0900 (JST) Received: from xproxy.gmail.com (xproxy.gmail.com [66.249.82.198]) by beryllium.ruby-lang.org (Postfix) with ESMTP id D00ED33D7A for ; Fri, 16 Dec 2005 02:32:12 +0900 (JST) Received: by xproxy.gmail.com with SMTP id t13so307079wxc for ; Thu, 15 Dec 2005 09:32:07 -0800 (PST) Received: by 10.70.126.10 with SMTP id y10mr2986035wxc; Thu, 15 Dec 2005 09:32:06 -0800 (PST) Received: by 10.70.53.9 with HTTP; Thu, 15 Dec 2005 09:32:06 -0800 (PST) Delivered-To: ruby-talk@ruby-lang.org Date: Fri, 16 Dec 2005 02:32:13 +0900 Posted: Thu, 15 Dec 2005 11:32:06 -0600 From: Eric Mahurin Reply-To: ruby-talk@ruby-lang.org Subject: Re: Easy Duck-Typing To: ruby-talk@ruby-lang.org (ruby-talk ML) Message-Id: <29256ea00512150932i26f52ff9jdfdce2532791655d@mail.gmail.com> In-Reply-To: <43a1914f$0$144$edfadb0f@dread11.news.tele.dk> References: <43a1914f$0$144$edfadb0f@dread11.news.tele.dk> X-ML-Name: ruby-talk X-Mail-Count: 76 X-MLServer: fml [fml 4.0.3 release (20011202/4.0.3)]; post only (only members can post) X-ML-Info: If you have a question, send e-mail with the body "help" (without quotes) to the address ruby-talk-ctl@ruby-lang.org; help= X-Original-To: ruby-talk@ruby-lang.org DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=CEGPkwBF9iimq/esQTV2+qtV0ja0jlBs0NkNMQoOfIGrptV4Q9O3HCC8aX5oLOeJHqivVc9qrBpBOriygkyizT1vuZon6d2Ex0TfQn0YIYn2LlDTSbmm2kFeB3DL+RldNEm5Ih5Vqrgkdv40is+r/voaVhlN/1oCSU6shH/0Yko= Content-Disposition: inline X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at ruby-lang.org X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on beryllium.ruby-lang.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=7.0 tests=AWL,BAYES_00, CONTENT_TYPE_PRESENT,MIMEQENC,QENCPTR1,QENCPTR2,RCVDFRMLOCALIP, RCVD_BY_IP autolearn=no version=3.0.3 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Precedence: bulk Lines: 32 List-Id: ruby-talk.ruby-lang.org List-Software: fml [fml 4.0.3 release (20011202/4.0.3)] List-Post: List-Owner: List-Help: List-Unsubscribe: X-Virus-Scanned: by AMaViS snapshot-20020531 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by blade.nagaokaut.ac.jp id jBFHWHZk029623 On 12/15/05, Daniel Schierbeck wrote: > class Kernel > def cast(obj, klass, method = nil) > return obj if obj.kind_of? klass > > method = "to_#{method || klass.downcase}" > > unless obj.respond_to? method > raise TypeError, "Can't convert #{obj.class} into #{klass}" > end > > cast_obj = obj.send(method) > > unless ccast_obj.kind_of? klass > raise TypeError, "#{obj.class}##{method} should return #{klass}" > end > > return cast_obj > end > end Like David said this is basically the opposite of duck-typing. The idea of duck-typing is that the code should completely ignore the type (however you want to define it). You just let ruby raise an exception if the object doesn't respond to the methods you need from the objects. I've heard the misconception before that duck-typing is most useful for type casting (i.e. #to_s method). To me, that is just a conveinence (the caller could just as easy have explicitly done the conversion). That is not where the power and beauty of duck-typing lies. It really is a quite flexible polymorphic concept.