Created
September 23, 2010 20:22
-
-
Save Whoops/594281 to your computer and use it in GitHub Desktop.
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
foo='foo' | |
bar='bar' | |
class A | |
def self.call_block | |
foo="I've got a lovely bunch of coconuts" | |
bar='detitly doo' | |
#note, yield calls the block assigned to a function | |
yield foo, bar | |
end | |
end | |
#this is our closure | |
#everything between do, end is our block | |
#this will print 'foobar' because | |
#the block retains it's original scope, | |
#so foo='foo' rather than | |
#"I've got a lovely bunch of coconuts" | |
#even though it was reassigned in the function | |
A.call_block do | |
puts "foo+bar=#{foo}#{bar}" | |
end | |
#an example of passing parameters into a block | |
#a="I've got a lovely bunch of coconuts" | |
#b='detitly doo' | |
#because yield foo, bar passed the | |
#*functions* local foo, bar into the block | |
#as parameters a,b | |
A.call_block do |a,b| | |
puts a | |
puts b | |
puts foo | |
puts bar | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment