Project

General

Profile

Actions

Bug #12478

closed

ruby2.2.5でオブジェクトに対して def_delegatorを行うとNoMethodError undefined method `method_defined?' が発生する。

Added by jy5kisok@gmail.com (keiichi ishioka) almost 8 years ago. Updated about 7 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-dev:49656]

Description

  • 再現コード

    printer = String.new
    printer.extend SingleForwardable
    printer.def_delegator "STDOUT", :puts
    # NoMethodError: undefined method `method_defined?' for "":String
    
  • 原因

  • def_single_delegatorで "method_defined?"を行っていますが、

  • Moduleクラスをmix-inしていないオブジェクトの場合NoMethodErrorになる。

  • 回避方法案

    • Moduleが該当オブジェクトのclassに属していない場合,該当処理をスキップする

      ```diff
      --- /usr/local/.rbenv/versions/2.2.5/lib/ruby/2.2.0/forwardable.rb.org  2016-06-10 10:31:09.613979605 +0900
      +++ /usr/local/.rbenv/versions/2.2.5/lib/ruby/2.2.0/forwardable.rb      2016-06-10 10:35:06.452506085 +0900
      @@ -277,7 +277,7 @@
          accessor = accessor.to_s
          if method_defined?(accessor) || private_method_defined?(accessor)
            accessor = "#{accessor}()"
      -    end if
      +    end if self.class === Module
      
          line_no = __LINE__; str = %{
            def #{ali}(*args, &block)
      ```
      
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0