Skip to content

Instantly share code, notes, and snippets.

Created December 7, 2012 11:25
Show Gist options
  • Save anonymous/4232658 to your computer and use it in GitHub Desktop.
Save anonymous/4232658 to your computer and use it in GitHub Desktop.
ruby variable and blocks
def pr(n)
puts n
sleep 1
puts n
end
# outputs 1 2 1 2
a = 1
Thread.new{pr(a)}
sleep 0.2
a=2
Thread.new{pr(a)}
sleep 2
# outputs 1 2 2 2 because a is shared between blocks / threads
a = 1
Thread.new{puts a; sleep 1; puts a}
sleep 0.2
a=2
Thread.new{puts a; sleep 1; puts a}
sleep 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment