Defining self-reflexive operators in Ruby

In the same way that a += 1 means a = a+1
is it possible in Ruby to create an operator such as .! (or something
like that) such that
someObject.!someMethod gives the same result as
someObject = someObject.someMethod

In the same way that a += 1 means a = a+1
is it possible in Ruby to create an operator such as .! (or something
like that) such that
someObject.!someMethod gives the same result as
someObject = someObject.someMethod

I’ve often wondered the same thing, but could never figure out an
elegant syntax for it.

On 09/08/2010 03:16 AM, Harry S. wrote:

In the same way that a += 1 means a = a+1
is it possible in Ruby to create an operator such as .! (or something
like that) such that
someObject.!someMethod gives the same result as
someObject = someObject.someMethod

Even if it was (but see the other replies) it would not be a good idea.
The reason is this:

class BangAndNoBang
def foo
x = dup
x.foo!
x
end

def foo!
# work
end
end

In other words: for efficiency reasons you would always implement method
x in terms of x! instead the other way round.

Kind regards

robert

On Sep 7, 2010, at 18:16 , Harry S. wrote:

In the same way that a += 1 means a = a+1
is it possible in Ruby to create an operator such as .! (or something
like that) such that
someObject.!someMethod gives the same result as
someObject = someObject.someMethod

no.

someObject isn’t the object, it is just a local variable reference to
it. Objects don’t know what variables or other objects refer to them.
Ruby’s syntax is opaque, so it wouldn’t be possible to write something
(in ruby, which is what you asked for) that could operate at parse-time
in this way (without fully pre-parsing, munging, and re-eval’ing. At
that stage, I wouldn’t call it “in ruby”).