# Defining self-reflexive operators in Ruby

**URL:** <https://www.ruby-forum.com/t/defining-self-reflexive-operators-in-ruby/194620>\
**Category:** Ruby\
**Created:** [September 8, 2010, 3:16am UTC](https://www.ruby-forum.com/t/defining-self-reflexive-operators-in-ruby/194620 "2010-09-08T03:16:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [September 8, 2010, 3:16am UTC](https://www.ruby-forum.com/t/defining-self-reflexive-operators-in-ruby/194620/1 "2010-09-08T03:16:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [September 8, 2010, 5:55am UTC](https://www.ruby-forum.com/t/defining-self-reflexive-operators-in-ruby/194620/2 "2010-09-08T05:55:00Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [September 8, 2010, 8:17am UTC](https://www.ruby-forum.com/t/defining-self-reflexive-operators-in-ruby/194620/3 "2010-09-08T08:17:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [September 8, 2010, 6:44am UTC](https://www.ruby-forum.com/t/defining-self-reflexive-operators-in-ruby/194620/4 "2010-09-08T06:44:54Z")

</div>

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”).
