From ruby-talk-admin@ruby-lang.org Fri Dec 16 00:57:49 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 jBFFvnZk026796; Fri, 16 Dec 2005 00:57:49 +0900 Received: from funfun.nagaokaut.ac.jp (funfun.nagaokaut.ac.jp [133.44.2.201]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 482595D86; Fri, 16 Dec 2005 00:57:50 +0900 (JST) Received: from localhost (localhost.nagaokaut.ac.jp [127.0.0.1]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id 250DBF04897; Fri, 16 Dec 2005 00:57:54 +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 C97B7F04896; Fri, 16 Dec 2005 00:57:52 +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 8EBB1630024; Fri, 16 Dec 2005 00:57:55 +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 2394333EF4; Fri, 16 Dec 2005 00:57:50 +0900 (JST) Received: from localhost (beryllium.ruby-lang.org [127.0.0.1]) by beryllium.ruby-lang.org (Postfix) with ESMTP id C4C2033F1C for ; Fri, 16 Dec 2005 00:57:39 +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 16247-02 for ; Fri, 16 Dec 2005 00:57:39 +0900 (JST) Received: from sinus.lauschmusik.de (sinus.lauschmusik.de [213.95.32.201]) by beryllium.ruby-lang.org (Postfix) with ESMTP id 578B033EF4 for ; Fri, 16 Dec 2005 00:57:39 +0900 (JST) Received: from localhost ([127.0.0.1] helo=localhost.localdomain) by sinus.lauschmusik.de with esmtp (Exim 4.50) id 1EmvRd-0004gv-Fk for ruby-talk@ruby-lang.org; Thu, 15 Dec 2005 16:55:01 +0100 Received: from feed118.news.tele.dk (feed118.news.tele.dk) by sinus.lauschmusik.de with NTTP id 166219 for ; Thu Dec 15 16:55:01 CET 2005 Received: from [87.48.73.133] by feed118.news.tele.dk (unknown) with NNTP id 166219 for ; Thu Dec 15 16:55:01 CET 2005 Received: from Usenet via a Usenet to mail gateway located at lauschmusik.de. This service provided as a courtesy to the ruby-talk mailing list. If this message is SPAM, its ultimate origin is Usenet, not this gateway program. All subscribers to the ruby-talk mailing list agree to receive the Usenet postings made to comp.lang.ruby via this gateway. Please see http://www.ruby-lang.org/ruby-talk-usenet-policy.html. Delivered-To: ruby-talk@ruby-lang.org Date: Fri, 16 Dec 2005 00:57:40 +0900 Posted: Thu, 15 Dec 2005 16:52:51 +0100 From: Daniel Schierbeck Reply-To: ruby-talk@ruby-lang.org Subject: Easy Duck-Typing To: ruby-talk@ruby-lang.org (ruby-talk ML) Message-Id: <43a1914f$0$144$edfadb0f@dread11.news.tele.dk> X-ML-Name: ruby-talk X-Mail-Count: 53 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= User-Agent: Thunderbird 1.5 (X11/20051025) X-Original-To: ruby-talk@ruby-lang.org Path: noris.net!npeer.de.kpn-eurorings.net!news.tele.dk!news.tele.dk!small.news.tele.dk!feed118.news.tele.dk!not-for-mail Newsgroups: comp.lang.ruby Organization: TDC Totalloesninger NNTP-Posting-Host: 87.48.73.133 X-Trace: 1134661967 dread11.news.tele.dk 144 87.48.73.133:53889 X-Complaints-To: abuse@post.tele.dk X-From-Usenet: see Received: header above. X-rubymirror: yes 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=-5.9 required=7.0 tests=AWL,BAYES_00, CONTENT_TYPE_PRESENT,MSGID_FROM_MTA_HEADER,RCVDFRMLOCALIP autolearn=ham version=3.0.3 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk Lines: 44 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 This will make it easier to add type checking to your methods. Let me know if you think the method belongs in Object or another class, rather than in the Kernel module. 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 class Klass def foo(arg) # Will raise an exception unless `arg' is # of type SomeKlass, or if it has a method # #to_some_klass that returns an object of # type SomeKlass arg = cast(arg, SomeKlass, :some_klass) end end Is there an easy way to turn a CapitalizedName into a downcase_name? Right now I'm just downcasing the class name if no cast method name is explicitly provided, but I'm not sure that's enough. Cheers, Daniel