Time for a Ruby setter/getter pair!

def bar
  @bar
end

def bar=(b)
  @bar = b
end

Seems workable enough, right?

1.9.3p194 :008 > bar
 => nil 
1.9.3p194 :009 > bar=100
 => 100 
1.9.3p194 :010 > bar
 => 100 
1.9.3p194 :011 > self.bar
 => nil 

Uh. Huh?

1.9.3p194 :014 > self.bar=200
 => 200 
1.9.3p194 :015 > self.bar
 => 200 
1.9.3p194 :016 > bar
 => 100 

Wha?

1.9.3p194 :017 > local_variables
 => [:bar, :_] 

Oh. If you try to use the setter unadorned by an object - like, say, in another method in the same object - you end up making a local variable instead. I’m sure there’s a good reason for this, but I really cannot think of what it possibly could be right now. Sent me down a rathole for about 4 hours.

This has the neat side effect of making it impossible for “foo=” style methods to be private. So if you want to make your setter private for some reason - like say the getter is read-only to other objects - you need to name it something else. Meh.