From: rubikitch Date: 2005-08-14T19:28:12+09:00 Subject: [ruby-list:41037] Re: RubyにはPerlのuse strictのようなものはありませんか? From: Yukihiro Matsumoto Subject: [ruby-list:41036] Re: RubyにはPerlのuse strictのようなものはありませんか? Date: Sun, 14 Aug 2005 18:54:41 +0900 るびきちです。 > DuckTypingが適用できなくなるのでお薦めしません。たとえば、IO > を要求するときに > > raise TypeError unless IO === x > > と書いてあったらStringIOが適用できなくなります。もったいない。 そうなんですよね。 だから自分も限られた場面でしか使っていません。 たとえばHashによる疑似キーワード引数を使うメソッドの場合、 Hash === args or raise TypeError なんてのを書いたりしています。 テストのときに、さすがにargsに構造体を入れたり……はしないですよね。 あとは、とある事情で特定の型でないとだめという場合です。 El4r::ELInstanceクラスは、自分の知らないメソッドは、EmacsLisp関数に 投げられます。 そのとき、Emacsと通信することでEmacsLisp関数を呼んでいますが、 その前にタイプチェックすることは意味があることだと思っています。 module El4r class ELInstance def method_missing(name_or_lambda, *args, &block) # EmacsLisp関数へ投げる end def newbuf(x) Hash === x or raise ArgumentError, "argument must be a hash!" # ... check = lambda{|key, type| x[key] && (type===x[key] or raise TypeError) } with(:with_current_buffer, b) { # ... x[:contents] and insert x[:contents].to_s check[:line,Integer] and goto_line x[:line] check[:point,Integer] and goto_char x[:point] } # ... end end end るびきち☆ http://www.rubyist.net/~rubikitch/