LoginSignup
7
6

More than 5 years have passed since last update.

Refinementsで定義したメソッドをusingより前の行で呼び出すテクニック10選

Last updated at Posted at 2018-05-31

Refinementsはレキシカルスコープという説明をよく聞くけど、どこにコードが書かれているかというよりはusingを呼んだ後の環境で評価されるかどうかというのが重要。なのでコードの字面でRefinementsで定義したメソッドの呼び出しがusingより前に書かれていても、先にusingの方が評価されていれば呼び出せる。

Refinements are only active within a scope after the call to using.
https://docs.ruby-lang.org/en/2.5.0/syntax/refinements_rdoc.html

その1. BEGINの中でusing

BEGINusingを使うとどのコードよりも先にusingが実行されるため先頭行でusingを書いたのと同じように働く。

hi
# hi

BEGIN {
  using Module.new {
    refine(Object) do
      def hi
        puts "hi"
      end
    end
  }
}

その10. bindingを使う

evalの第二引数にusingが効いた環境のbindingを渡すとusingで定義したメソッドが使える。

def greeting(b)
  eval('hi', b)
end

using Module.new {
  refine(Object) do
    def hi
      puts "hi"
    end
  end
}

greeting(binding)

Binding#evalしてもよし。

def greeting(b)
  b.eval('hi')
end

using Module.new {
  refine(Object) do
    def hi
      puts "hi"
    end
  end
}

greeting(binding)

10ないじゃん

"10".to_i(2)
# => 2
7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6