rubyゴールドの資格勉強中にタイトルに関する問題を目にしたのですが、この両者の違いについて解説を読んでもいまひとつ理解できませんでした。
問題文としては以下になります。
m = Module.new m.instance_eval(<<-EOS) CONST = "instance_eval" def ins p Module.nesting CONST end EOS m.module_eval(<<-EOS) CONST = "module_eval" def mons p Module.nesting CONST end module_function :mons EOS p m.ins p m.mons
表示結果がいかになります。
[#<Class:#<Module:0x000000030abc18>>] "instance_eval" [#<Module:0x000000030abc18>] "module_eval"
疑問点
instance_evalはモジュールインスタンスの特異クラスを作成し、そこにメソッドやら定数やらを定義している?
module_evalはModuleに直接メソッドや定数を定義している?
あなたの回答
tips
プレビュー