{
    "mode": "ri",
    "parameter": "Concurrent::Atom",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/ri/Concurrent%3A%3AAtom/json",
    "generated": "2026-07-29T13:34:51Z",
    "sections": {
        "Concurrent::Atom < Synchronization::Object": {
            "content": "------------------------------------------------------------------------",
            "subsections": []
        },
        "Includes:": {
            "content": "Concern::Observable (from /home/chedong/.local/share/rdoc)\n\n(from /home/chedong/.local/share/rdoc)\n------------------------------------------------------------------------\nAtoms provide a way to manage shared, synchronous, independent state.\n\nAn atom is initialized with an initial value and an optional validation\nproc. At any time the value of the atom can be synchronously and safely\nchanged. If a validator is given at construction then any new value will\nbe checked against the validator and will be rejected if the validator\nreturns false or raises an exception.\n\nThere are two ways to change the value of an atom: {#compareandset}\nand {#swap}. The former will set the new value if and only if it\nvalidates and the current value matches the new value. The latter will\natomically set the new value to the result of running the given block if\nand only if that value validates.\n\n## Example\n\n``` def nextfibonacci(set = nil)\nreturn [0, 1] if set.nil?\nset + [set[-2..-1].reduce{|sum,x| sum + x }]\n\nend\n\n# create an atom with an initial value atom =\nConcurrent::Atom.new(nextfibonacci)\n\n# send a few update requests 5.times do\natom.swap{|set| nextfibonacci(set) }\n\nend\n\n# get the current value atom.value #=> [0, 1, 1, 2, 3, 5, 8] ```\n\n## Observation\n\nAtoms support observers through the {Concurrent::Observable} mixin\nmodule. Notification of observers occurs every time the value of the\nAtom changes. When notified the observer will receive three arguments:\ntime, oldvalue, and newvalue. The time argument is the time at which\nthe value change occurred. The oldvalue is the value of the Atom when\nthe change began The newvalue is the value to which the Atom was set\nwhen the change completed. Note that oldvalue and newvalue may be the\nsame. This is not an error. It simply means that the change operation\nreturned the same value.\n\nUnlike in Clojure, Atom cannot participate in {Concurrent::TVar}\ntransactions.\n\n@!macro threadsafevariablecomparison\n\n@see http://clojure.org/atoms Clojure Atoms @see\nhttp://clojure.org/state Values and Change - Clojure's approach to\nIdentity and State\n------------------------------------------------------------------------",
            "subsections": []
        },
        "Class methods:": {
            "content": "new\n",
            "subsections": []
        },
        "Instance methods:": {
            "content": "compareandset\nreset\nswap\n",
            "subsections": []
        }
    },
    "flags": [],
    "examples": [],
    "see_also": []
}