From: RichardOnRails Date: 2011-01-23T13:50:24+09:00 Subject: Easiest way in Ruby to express "given this, set target to this if nil or undefined, else increment by this" The following works, but I'd prefer not to have to class values: class Z def self.show_z @@zz ||= 0; @@zz += 1 puts @@zz end end Z.show_z # => 1 Z.show_z # => 2 I tried this: $h = Hash.new(:y) def show_y $h[:y] ||= 0; $h[:y] += 1 # => Error msg # Line 18 puts $h[:y] end show_y show_y but it doesn't pass muster with Ruby 1.8.6. I get a complaint about the first line in show_y, which makes no sense IMHO: TestOrEqual_operator.rb:18:in `show_y': undefined method `+' for :y:Symbol (NoMethodError) from TestOrEqual_operator.rb:21 Also, I'd be able to use a succinct version of this without resorting to globals. An ideas? Thanks in Advance, Richard