Created
July 29, 2013 16:43
-
-
Save kaspergrubbe/6105698 to your computer and use it in GitHub Desktop.
Ruby procs
This file contains hidden or 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
| class Worker | |
| def hello(work) | |
| work.call | |
| end | |
| def name(n) | |
| "Worker #{n}" | |
| end | |
| end | |
| class Master | |
| def test | |
| w = Worker.new | |
| w.hello Proc.new { | |
| puts name("Kasper") | |
| } | |
| end | |
| end | |
| m = Master.new | |
| m.test |
Author
Author
This fixed my issue:
class Worker
def hello(&block)
instance_eval &block
end
def name(n)
"Worker #{n}"
end
end
class Master
def test
w = Worker.new
w.hello { puts name("Kasper") }
end
end
m = Master.new
m.test
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gives this output: