0

私はそれらが含まれているときに「含まれている」関数を呼び出す必要があるモジュールをいくつか持っています..この関数はそれらのすべてで同じです..したがって、この含まれている関数をモジュールを作成し、これらすべてのサブモジュールにそれを含めます..これは不自然な例ですが、次のようなものが必要です....

module Humanable
  def self.extended(klass)
    klass.instance_eval do
      define_method :included do |base|
        puts 'I was just included into the Person Class'
      end
    end
  end
end

module Personable
  extend Human
end

class Person
  include Personable
end

Person に Personable が含まれている場合、Humanable モジュールから文字列を配置します。これは機能しませんが、どうすればこれを達成できるかがわかりますか?

4

1 に答える 1

0

答えは次のようです:

module Humanable
  def self.extended(klass)
    klass.define_singleton_method :included do |base|
      puts 'hello'
    end
  end
end

module Personable
  extend Humanable # does **not** puts 'hello'
end

class Person
  include Personable # puts 'hello'
end
于 2012-11-08T03:45:58.763 に答える