LoginSignup
5
3

More than 5 years have passed since last update.

block を yield で処理するのか Proc に変換して処理するのかで block 内での self が異なる

Posted at

最初のは yield での処理、その後は Proc オブジェクトに変換しての処理です。

class Foo
  def self.dsl1
    new.instance_eval { |x| yield(x) }
  end

  def self.dsl2(&block)
    new.instance_eval(&block)
  end

  def self.dsl3
    new.instance_eval(&proc)
  end

  def self.dsl4
    new.instance_eval(&Proc.new)
  end
end
Foo.dsl1 { p self }   => main
Foo.dsl2 { p self }   => #<Foo:0x002ac2f5ec84b0>
Foo.dsl3 { p self }   => #<Foo:0x002ac2f5ec8370>
Foo.dsl4 { p self }   => #<Foo:0x002ac2f5ec8208>

proc に変換して block を処理すると block のスコープが Foo のインスタンスになるからかなぁ?

5
3
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
5
3