No, <operator>=
can not be redefined in Ruby.
You can try to get really fancy and wrap your return values in classes that delegate to the actual value. This way, they behave like the actual value, but you can play tricks, for instance with +
.
Here's a simple example:
require 'delegate'
module Redis
class Set
class Value < SimpleDelegator
def +(val)
Increment.new(self, val)
end
end
class Increment < SimpleDelegator
attr_reader :increment
def initialize(source, increment)
super(source.__getobj__ + increment)
@increment = increment
end
end
def [](key)
Value.new(@redis.not_sure_what(@name, key))
end
def []=(key,val)
if val.is_a?(Increment)
@redis.zincrby(@name,val.increment,key)
else
@redis.zadd(@name,val,key)
end
end
end
end
This is just a starting point. You'll have to be more careful than this, for example by checking the key is the same. In my simplistic example, redis[:foo] = redis[:bar] + 1
would actually be equivalent to redis[:foo] += 1
...