Last active
March 5, 2021 02:39
-
-
Save holysugar/bb601d2670be5be6b1e3a0f4e72503e8 to your computer and use it in GitHub Desktop.
dsl-sample.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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