From: nobu@... Date: 2016-06-10T11:19:14+00:00 Subject: [ruby-dev:49659] [Ruby trunk Bug#12478] ruby2.2.5でオブジェクトに対して def_delegatorを行うとNoMethodError undefined method `method_defined?' が発生する。 Issue #12478 has been updated by Nobuyoshi Nakada. Description updated Backport changed from 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN to 2.1: DONTNEED, 2.2: REQUIRED, 2.3: REQUIRED ---------------------------------------- Bug #12478: ruby2.2.5でオブジェクトに対して def_delegatorを行うとNoMethodError undefined method `method_defined?' が発生する。 https://bugs.ruby-lang.org/issues/12478#change-59133 * Author: keiichi ishioka * Status: Open * Priority: Normal * Assignee: * ruby -v: 2.2.5 * Backport: 2.1: DONTNEED, 2.2: REQUIRED, 2.3: REQUIRED ---------------------------------------- - 再現コード ```ruby 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) ``` -- https://bugs.ruby-lang.org/