diff options
author | Xavier Noria <fxn@hashref.com> | 2010-02-07 11:22:25 +0100 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-02-07 11:22:44 +0100 |
commit | 81e779a0191c824f3e4c0df23736bdadb052ac7c (patch) | |
tree | a861c680999e4e2b49b61780bb924f148fd485a6 /railties/guides/source | |
parent | b3e43a64627ef0b1a31082d4daef13e98ce144bc (diff) | |
download | rails-81e779a0191c824f3e4c0df23736bdadb052ac7c.tar.gz rails-81e779a0191c824f3e4c0df23736bdadb052ac7c.tar.bz2 rails-81e779a0191c824f3e4c0df23736bdadb052ac7c.zip |
AS guide: documents Module#synchronize
Diffstat (limited to 'railties/guides/source')
-rw-r--r-- | railties/guides/source/active_support_core_extensions.textile | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index ed2b41af86..4f7c0c56e2 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -791,6 +791,30 @@ WARNING: This method is exact if running under Ruby 1.9. In previous versions it NOTE: Defined in +active_support/core_ext/module/introspection.rb+. +h4. Synchronization + +The +synchronize+ macro declares a method to be synchronized: + +<ruby> +class Counter + @@mutex = Mutex.new + attr_reader :value + + def initialize + @value = 0 + end + + def incr + @value += 1 # non-atomic + end + synchronize :incr, :with => '@@mutex' +end +</ruby> + +The method receives the name of an action, and a +:with+ option with code. The code is evaluated in the context of the receiver each time the method is invoked, and it should evaluate to a +Mutex+ instance or any other object that responds to +synchronize+ and accepts a block. + +NOTE: Defined in +active_support/core_ext/module/synchronization.rb+. + h3. Extensions to +Class+ h4. Class Attributes |