From: Eric Hodel Date: 2006-03-02T07:11:21+09:00 Subject: Re: Newby question: Is += atomic for integers? On Mar 1, 2006, at 2:08 PM, rj-cole wrote: > Hi, > > Can the following program produce counts that are wrong? > > class Test > attr_accessor :count > def initialize > @count = 0 > end > end > > $test = Test.new > > def my_function_called_by_many_threads > ... > $test.count += 1 > ... > end Yes. += is just syntax sugar. It gets expanded: $ parse_tree_show -f a += 1 [[:class, :Example, :Object, [:defn, :example, [:scope, [:block, [:args], [:lasgn, :a, [:call, [:lvar, :a], :+, [:array, [:lit, 1]]]]]]]]] > Any why is the following a syntax error? > > class Test > def +=(value) > end > end > > i.e why can't I define the += operator? += is not an operator, it is syntax sugar. -- Eric Hodel - drbrain@segment7.net - http://blog.segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com