なんとなく日々徒然と

instance_evalを引数つけて実行

ruby1.9からです。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Foo
  def initialize
    @v = 2
  end
end

foo = Foo.new
foo.instance_exec(2) do |bar|
  p @v * bar
end
# => 4

Tags:

sqlの実行。 ある特定のオブジェクトにメソッドを追加する

comments powered by Disqus