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


メタプログラミングRuby | 魔術 | クリーンルーム

内容

ブロックを評価する環境としてオブジェクトのinstance_evalを利用します。
主にDSLで利用されます。

サンプル

RSpec風のDSLを作成してみます。
instance_evalをそのまま利用するとかっこ悪いので
エイリアスで偽装してみます。

# encoding: utf-8

class RSpec
  class << self
    alias spec instance_eval
    def describe(value, &block)
      puts value
      block.call
    end

    def it(value, &block)
      puts value
      block.call
    end
  end
end
R = RSpec

R.spec do
  puts "before describe"

  describe "describe" do
    puts "before it"

    it "it" do
      puts "after it"
      
    end
  end
end

出力

before describe
describe
before it
it
after it



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

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