Makes it easy to create and use thread-local variables. This encapsulates two patterns:
Thread.currenthash is not namespaced, which can make name collision between different libraries or parts of an app very possible. ThreadVariable gets around this by namespacing variables under the current class/module name.
- Accessing and setting
Thread.currentis not so pretty to look at, so ThreadVariable creates a getter and setter for each thread variable.
Install as your normally would, nothing special here. Only compatible with Ruby 1.9+.
extend ThreadVariable and call
thread_variable in classes you want thread variables in:
class C extend thread_variable :foo end C.foo = 'bar' Thread.new do C.foo = 'baz' C.foo #=> "baz" end.join C.foo #=> "bar"
If you don’t want to
extend ThreadVariable everywhere you can instead do
include ThreadVariable in both
For more, just read the source. Seriously, go read it—it’s less than 20 lines and will only take a minute.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request
Credits & License
Copyright © 2012 J. Andrew Marshall. All rights reserved. License is available in the LICENSE file.