Skip to content

Instantly share code, notes, and snippets.

@holysugar
Last active March 5, 2021 02:39
Show Gist options
  • Save holysugar/bb601d2670be5be6b1e3a0f4e72503e8 to your computer and use it in GitHub Desktop.
Save holysugar/bb601d2670be5be6b1e3a0f4e72503e8 to your computer and use it in GitHub Desktop.
dsl-sample.rb
def simpleblock(&block)
block.call
end
class Sandbox # こういうときは BasicObject 継承がいいと思うけど説明の都合でこのまま
end
def sandbox(&block)
Sandbox.new.instance_eval(&block)
end
def foo
'main'
end
simpleblock do
def foo
'simple'
end
p foo #=> "simple"
p self #=> main
p self.method(:foo) #=> #<Method: Object#foo() hoge.rb:17>
end
sandbox do
def foo
'foo'
end
p foo #=> "foo"
p self #=> #<Sandbox:0x00007f9bec0a7000>
p self.method(:foo) #=> #<Sandbox:0x00007f9bec0a7000>.foo() hoge.rb:27>
end
p foo #=> "simple"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment