Created
March 8, 2012 05:32
-
-
Save jonnyzheng/1998921 to your computer and use it in GitHub Desktop.
Proc and lambda
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
class Testclass | |
def mytest(&block) | |
result = block.call | |
return result * 2 | |
end | |
end | |
def myclient | |
myproc = Proc.new{return 10} | |
test = Testclass.new | |
puts test.mytest(&myproc) | |
# 123 would never be print | |
puts '123' | |
end | |
# will print 10 | |
puts myclient |
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
class Testclass | |
def mytest(&block) | |
result = block.call | |
return result * 2 | |
end | |
end | |
myproc = Proc.new{return 10} | |
test = Testclass.new | |
# will print: `block in <main>': unexpected return (LocalJumpError) | |
puts test.mytest(&myproc) |
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
class Testclass | |
def mytest(&block) | |
result = block.call | |
return result * 2 | |
end | |
end | |
myproc = lambda{return 10} | |
test = Testclass.new | |
# will print: 20 | |
puts test.mytest(&myproc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment