以下の内容はhttps://tbpgr.hatenablog.com/entry/20140726/1406382870より取得しました。


Ruby | Module | module_exec/class_exec

概要

Module#module_exec(*args) {|*vars| ... } -> object[permalink][rdoc]
Module#class_exec(*args) {|*vars| ... } -> object

詳細

与えられたブロックを指定された args を引数としてモジュールのコンテキストで評価する。

サンプルコード
module Hogeable
  def hogeable
  end
end

class Hoge
  include Hogeable
  def hoge
    print 'hoge'
  end
end

CONST = 'const'
Hoge.module_exec(CONST) do |args|
  const_set(:HOGE_CONST, args)
  def hoge2
    puts Hoge::HOGE_CONST + 'hoge2'
  end
end

Hoge.new.hoge2

出力

出力

consthoge2



以上の内容はhttps://tbpgr.hatenablog.com/entry/20140726/1406382870より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14