Rubyのモジュールをおさらい
module Foo def hello puts "hello" end end Foo::hello #-> undefined method `hello' for Foo:Module (NoMethodError)
includeしないと使えないのかな…
module Foo def hello puts "hello" end end module Bar include Foo def hello2 hello hello end end def tmp include Bar hello2 end tmp hello # 呼べる
うーむ、includeすれば使えるは使えるが、includeはモジュールの中のメソッドをクラスに追加するのであって、メソッドのローカル変数として関数オブジェクトをインポートするのとは違うんだなぁ。これだと実質的に「インポートしたモジュールの中身はグローバルスコープにぶちまけられます」って状態だよねぇ
module Foo def hello puts "hello" end end module Bar include Foo def hello Foo::hello Foo::hello end end include Bar Bar::hello #-> stack level too deep (SystemStackError)
ああー。やっぱどっちのモジュール由来かとか忘れてincludeの時点で単純にObjectに混ぜ込んでしまってるんだなぁ。モジュールで継承&オーバーライドは無理ってことでファイナルアンサー??
@knu っmodule_function
module Foo def hello puts "hello" end module_function :hello end module Bar def hello Foo.hello Foo.hello end module_function :hello end Bar.hello
なるほど、module_functionをよんでやらないとモジュールオブジェクトのメンバにならないのか。