Skip to content

Instantly share code, notes, and snippets.

@Whoops
Created September 23, 2010 20:22
Show Gist options
  • Save Whoops/594281 to your computer and use it in GitHub Desktop.
Save Whoops/594281 to your computer and use it in GitHub Desktop.
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