Module#define_methodとsuper

define_methodの中にsuperを書くとどうなるか実験。

class A
  def hoge
    "hoge"
  end
  
end

class B < A
  define_method :hoge do
    "[#{super}]"
  end
end

class C < A
  define_method :hoge do
    "[#{super()}]"
  end
end

RUBY_VERSION           # => "1.8.7"
B.new.hoge rescue $!   # => "[hoge]"
C.new.hoge             # => "[hoge]"

Ruby 1.8.7だと動く。

class A
  def hoge
    "hoge"
  end
  
end

class B < A
  define_method :hoge do
    "[#{super}]"
  end
end

class C < A
  define_method :hoge do
    "[#{super()}]"
  end
end

RUBY_VERSION           # => "1.9.0"
B.new.hoge rescue $!   # => #<RuntimeError: implicit argument passing of super from method defined by define_method() is not supported. Specify all arguments explicitly.>
C.new.hoge             # => "[hoge]"

Ruby 1.9だと「super」はエラーで、明示的に引数をつけないといけないようだ。なんだろう……

追記

だいありー

ささださんより返事をいただきました。

どーしても実装出来なかったからです.誰か,実装方 法を教えてください.

なるほど、実装上の都合ですかぁ。

コメントできなかったので,こちらで

スパムコメント防止のためにやむなくはてなユーザーのみコメントを許可しています。御了承ください。