# Operator Overloading

**URL:** <https://rubytalk.org/t/operator-overloading/63758>\
**Category:** ruby-talk\
**Created:** [4 October 2011 05:11 UTC](https://rubytalk.org/t/operator-overloading/63758 "2011-10-04T05:11:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thescholar\_Theschola](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Thescholar\_Theschola](https://rubytalk.org/u/Thescholar_Theschola)\
**Post date:** [4 October 2011 05:11 UTC](https://rubytalk.org/t/operator-overloading/63758/1 "2011-10-04T05:11:02Z")

</div>

Let's suppose I have a class like this one and then I create two  
objects:

> **···**
>
> =-=-=-=-=-=-=-=-=-=-=-=  
> class Point
> 
> &nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;x = 5  
> &nbsp;&nbsp;&nbsp;&nbsp;y = 10  
> &nbsp;&nbsp;&nbsp;&nbsp;z = 25  
> &nbsp;&nbsp;end
> 
> end
> 
> a = Point.new  
> b = Point.new  
> =-=-=-=-=-=-=-=-=-=-=-=
> 
> How do I do if I want to add a and b together to a new point called c?  
> Example:
> 
> c = a + b
> 
> How exactly should I implement the operator overloading feature? So that  
> x = 10, y = 20, z = 50. There are hardly any tutorial for this...
> 
> Thank you!
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Phillip\_Gawlowski](https://avatars.discourse-cdn.com/v4/letter/p/ecc23a/32.png) [@Phillip\_Gawlowski](https://rubytalk.org/u/Phillip_Gawlowski)\
**Post date:** [4 October 2011 05:22 UTC](https://rubytalk.org/t/operator-overloading/63758/2 "2011-10-04T05:22:55Z")

</div>

Ruby doesn't do operator overloading. But that doesn't matter, since  
"+" is a method. Thus:

class Point  
&nbsp;&nbsp;def self.+(obj)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Details of addition  
&nbsp;&nbsp;end  
end

> **···**
>
> On Tue, Oct 4, 2011 at 7:11 AM, Thescholar Thescholar \<thescholar@hotmail.ca\> wrote:
> 
> > Let's suppose I have a class like this one and then I create two  
> > objects:
> > 
> > =-=-=-=-=-=-=-=-=-=-=-=  
> > class Point
> > 
> > def initialize  
> > x = 5  
> > y = 10  
> > z = 25  
> > end
> > 
> > end
> > 
> > a = Point.new  
> > b = Point.new  
> > =-=-=-=-=-=-=-=-=-=-=-=
> > 
> > How do I do if I want to add a and b together to a new point called c?  
> > Example:
> > 
> > c = a + b
> > 
> > How exactly should I implement the operator overloading feature? So that  
> > x = 10, y = 20, z = 50. There are hardly any tutorial for this...
> 
> --  
> Phillip Gawlowski
> 
> gplus.to/phgaw | twitter.com/phgaw
> 
> A method of solution is perfect if we can forsee from the start,  
> and even prove, that following that method we shall attain our aim.  
> -- Leibniz

---

<div class="post-metadata">

**Author:** ![Hans\_Mackowiak](https://avatars.discourse-cdn.com/v4/letter/h/f08c70/32.png) [@Hans\_Mackowiak](https://rubytalk.org/u/Hans_Mackowiak)\
**Post date:** [4 October 2011 05:23 UTC](https://rubytalk.org/t/operator-overloading/63758/3 "2011-10-04T05:23:57Z")

</div>

class Point  
&nbsp;&nbsp;def +(other)  
&nbsp;&nbsp;&nbsp;&nbsp;#there your code, i think  
&nbsp;&nbsp;&nbsp;&nbsp;result = Point.new  
&nbsp;&nbsp;&nbsp;&nbsp;result.x = self.x + other.x  
&nbsp;&nbsp;&nbsp;&nbsp;result.y = self.y + other.y  
&nbsp;&nbsp;&nbsp;&nbsp;result.z = self.z + other.z  
&nbsp;&nbsp;&nbsp;&nbsp;return result  
&nbsp;&nbsp;end  
end

but you also need to make the variables accessable like  
class Point  
&nbsp;&nbsp;attr\_accessor :x,:y,:z  
end

and you should remove your init, it is wrong

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)\
**Post date:** [4 October 2011 05:26 UTC](https://rubytalk.org/t/operator-overloading/63758/4 "2011-10-04T05:26:24Z")

</div>

class Point  
&nbsp;&nbsp;attr\_accessor :x, :y, :z

def initialize(x=5, y=10, z=25)  
&nbsp;&nbsp;&nbsp;self.x = x  
&nbsp;&nbsp;&nbsp;self.y = y  
&nbsp;&nbsp;&nbsp;self.z = z  
end

def +(point)  
&nbsp;&nbsp;&nbsp;Point.new x+point.x, y+point.y, z+point.z  
end

def inspect  
&nbsp;&nbsp;&nbsp;"(#{x}, #{y}, #{z})"  
end  
end

a = Point.new  
b = Point.new

a # =\> (5, 10, 25)  
b # =\> (5, 10, 25)  
a + b # =\> (10, 20, 50)

> **···**
>
> On Tue, Oct 4, 2011 at 12:11 AM, Thescholar Thescholar \< thescholar@hotmail.ca\> wrote:
> 
> > Let's suppose I have a class like this one and then I create two  
> > objects:
> > 
> > =-=-=-=-=-=-=-=-=-=-=-=  
> > class Point
> > 
> > def initialize  
> > &nbsp;&nbsp;&nbsp;x = 5  
> > &nbsp;&nbsp;&nbsp;y = 10  
> > &nbsp;&nbsp;&nbsp;z = 25  
> > end
> > 
> > end
> > 
> > a = Point.new  
> > b = Point.new  
> > =-=-=-=-=-=-=-=-=-=-=-=
> > 
> > How do I do if I want to add a and b together to a new point called c?  
> > Example:
> > 
> > c = a + b
> > 
> > How exactly should I implement the operator overloading feature? So that  
> > x = 10, y = 20, z = 50. There are hardly any tutorial for this...
> > 
> > Thank you!
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [4 October 2011 06:06 UTC](https://rubytalk.org/t/operator-overloading/63758/5 "2011-10-04T06:06:19Z")

</div>

For a full blown example you can look here:  
[http://blog.rubybestpractices.com/posts/rklemme/019-Complete\_Numeric\_Class.html](http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html)

Kind regards

robert

> **···**
>
> On Tue, Oct 4, 2011 at 7:11 AM, Thescholar Thescholar \<thescholar@hotmail.ca\> wrote:
> 
> > Let's suppose I have a class like this one and then I create two  
> > objects:
> > 
> > =-=-=-=-=-=-=-=-=-=-=-=  
> > class Point
> > 
> > def initialize  
> > x = 5  
> > y = 10  
> > z = 25  
> > end
> > 
> > end
> > 
> > a = Point.new  
> > b = Point.new  
> > =-=-=-=-=-=-=-=-=-=-=-=
> > 
> > How do I do if I want to add a and b together to a new point called c?  
> > Example:
> > 
> > c = a + b
> > 
> > How exactly should I implement the operator overloading feature? So that  
> > x = 10, y = 20, z = 50. There are hardly any tutorial for this...
> > 
> > Thank you!
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Darryl\_L\_Pierce](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Darryl\_L\_Pierce](https://rubytalk.org/u/Darryl_L_Pierce)\
**Post date:** [4 October 2011 13:15 UTC](https://rubytalk.org/t/operator-overloading/63758/6 "2011-10-04T13:15:57Z")

</div>

> Let's suppose I have a class like this one and then I create two  
> objects:
> 
> =-=-=-=-=-=-=-=-=-=-=-=  
> class Point
> 
> &nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;x = 5  
> &nbsp;&nbsp;&nbsp;&nbsp;y = 10  
> &nbsp;&nbsp;&nbsp;&nbsp;z = 25  
> &nbsp;&nbsp;end

This won't work since x, y and z in the above are local to the  
constructor. So those variables will be lost when the constructor exits.

> end
> 
> a = Point.new  
> b = Point.new  
> =-=-=-=-=-=-=-=-=-=-=-=
> 
> How do I do if I want to add a and b together to a new point called c?  
> Example:
> 
> c = a + b
> 
> How exactly should I implement the operator overloading feature? So that  
> x = 10, y = 20, z = 50. There are hardly any tutorial for this...

Define a + method on your Point class:

def +(other)  
&nbsp;&nbsp;self.x += other.x  
&nbsp;&nbsp;self.y += other.y  
&nbsp;&nbsp;self.z += other.z  
&nbsp;&nbsp;self  
end

That last line is IMPORTANT. Remember, Ruby methods treat the last value  
evaluated in a method as the return value. So if you don't put self in  
then the return value for the + method will be the last addition.

So now with the above:

c = a + b

is the equivalent of doing:

c = a.+(b)

HTH.

> **···**
>
> On 10/04/2011 01:11 AM, Thescholar Thescholar wrote:
> 
> --  
> Darryl L. Pierce \<mcpierce@gmail.com\>  
> [http://mcpierce.multiply.com/](http://mcpierce.multiply.com/)  
> "What do you care what people think, Mr. Feynman?"

---

<div class="post-metadata">

**Author:** ![Darryl\_L\_Pierce](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Darryl\_L\_Pierce](https://rubytalk.org/u/Darryl_L_Pierce)\
**Post date:** [4 October 2011 13:16 UTC](https://rubytalk.org/t/operator-overloading/63758/7 "2011-10-04T13:16:54Z")

</div>

> class Point  
> &nbsp;&nbsp;def +(other)  
> &nbsp;&nbsp;&nbsp;&nbsp;#there your code, i think  
> &nbsp;&nbsp;&nbsp;&nbsp;result = Point.new  
> &nbsp;&nbsp;&nbsp;&nbsp;result.x = self.x + other.x  
> &nbsp;&nbsp;&nbsp;&nbsp;result.y = self.y + other.y  
> &nbsp;&nbsp;&nbsp;&nbsp;result.z = self.z + other.z  
> &nbsp;&nbsp;&nbsp;&nbsp;return result  
> &nbsp;&nbsp;end  
> end
> 
> but you also need to make the variables accessable like  
> class Point  
> &nbsp;&nbsp;attr\_accessor :x,:y,:z  
> end

And add '@' to them in the constructor.

> **···**
>
> On 10/04/2011 01:23 AM, Hans Mackowiak wrote:
> 
> > and you should remove your init, it is wrong
> 
> --  
> Darryl L. Pierce \<mcpierce@gmail.com\>  
> [http://mcpierce.multiply.com/](http://mcpierce.multiply.com/)  
> "What do you care what people think, Mr. Feynman?"

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [4 October 2011 14:03 UTC](https://rubytalk.org/t/operator-overloading/63758/8 "2011-10-04T14:03:11Z")

</div>

Darryl Pierce wrote in post #1024950:

> Define a + method on your Point class:
> 
> def +(other)  
> &nbsp;&nbsp;self.x += other.x  
> &nbsp;&nbsp;self.y += other.y  
> &nbsp;&nbsp;self.z += other.z  
> &nbsp;&nbsp;self  
> end

Warning!! That will \*mutate\* the point passed as the left-hand argument  
of the '+'!

I would suggest instead:

def +(other)  
&nbsp;&nbsp;Point.new(x+other.x, y+other.y, z+other.z)  
end

(Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you  
think it's clearer)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Darryl\_L\_Pierce](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Darryl\_L\_Pierce](https://rubytalk.org/u/Darryl_L_Pierce)\
**Post date:** [4 October 2011 14:53 UTC](https://rubytalk.org/t/operator-overloading/63758/9 "2011-10-04T14:53:09Z")

</div>

> Darryl Pierce wrote in post #1024950:
> 
> > Define a + method on your Point class:
> > 
> > def +(other)  
> > &nbsp;&nbsp;self.x += other.x  
> > &nbsp;&nbsp;self.y += other.y  
> > &nbsp;&nbsp;self.z += other.z  
> > &nbsp;&nbsp;self  
> > end
> 
> Warning!! That will \*mutate\* the point passed as the left-hand argument  
> of the '+'!
> 
> I would suggest instead:
> 
> def +(other)  
> &nbsp;&nbsp;Point.new(x+other.x, y+other.y, z+other.z)  
> end

DOH! Good point!

> (Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you  
> think it's clearer)

I don't think so. 'x' is a local variable, while '@x' is an instance  
variable. And since the fields are instance variables they have to be  
prefixed with '@'.

> **···**
>
> On 10/04/2011 10:03 AM, Brian Candler wrote:
> 
> --  
> Darryl L. Pierce \<mcpierce@gmail.com\>  
> [http://mcpierce.multiply.com/](http://mcpierce.multiply.com/)  
> "What do you care what people think, Mr. Feynman?"

---

<div class="post-metadata">

**Author:** ![Dave\_Aronson1](https://avatars.discourse-cdn.com/v4/letter/d/8e7dd6/32.png) [@Dave\_Aronson1](https://rubytalk.org/u/Dave_Aronson1)\
**Post date:** [4 October 2011 15:52 UTC](https://rubytalk.org/t/operator-overloading/63758/10 "2011-10-04T15:52:30Z")

</div>

Not quite. This is one of Ruby's little gotchas. A naked "x" will be  
taken as a local variable in the method. If you want to use an  
instance variable you \*must\* precede it with either "self." or "@".

-Dave

> **···**
>
> On Tue, Oct 4, 2011 at 10:03, Brian Candler \<b.candler@pobox.com\> wrote:
> 
> > (Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you  
> > think it's clearer)
> 
> --  
> LOOKING FOR WORK, preferably Ruby on Rails, in NoVa/DC; see main web site.  
> Main Web Site: davearonson.com  
> Programming Blog: codosaur.us  
> Excellence Blog: dare2xl.com

---

<div class="post-metadata">

**Author:** ![Adam\_Prescott](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@Adam\_Prescott](https://rubytalk.org/u/Adam_Prescott)\
**Post date:** [4 October 2011 15:11 UTC](https://rubytalk.org/t/operator-overloading/63758/11 "2011-10-04T15:11:22Z")

</div>

Not quite. 🙂

class Foo  
&nbsp;&nbsp;attr\_reader :x

&nbsp;&nbsp;def initialize(x)  
&nbsp;&nbsp;&nbsp;&nbsp;@x = x  
&nbsp;&nbsp;end

&nbsp;&nbsp;def bar  
&nbsp;&nbsp;&nbsp;&nbsp;x # 🙂  
&nbsp;&nbsp;end  
end

f = Foo.new(1)  
puts f.bar

[http://ideone.com/mbPc0](http://ideone.com/mbPc0)

If there's no local variable in the scope, the interpreter sees it as a  
method call. If it's ambiguous, it'll pick the variable over the method  
call, unless you force it with a trailing ().

x = 1  
def x; 2; end  
puts x  
puts x()

> **···**
>
> On Tue, Oct 4, 2011 at 3:53 PM, Darryl L. Pierce \<mcpierce@gmail.com\> wrote:
> 
> > \> (Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you  
> > \> think it's clearer)
> > 
> > I don't think so. 'x' is a local variable, while '@x' is an instance  
> > variable. And since the fields are instance variables they have to be  
> > prefixed with '@'.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [4 October 2011 15:58 UTC](https://rubytalk.org/t/operator-overloading/63758/12 "2011-10-04T15:58:46Z")

</div>

The story is a bit more complex.

1. writing

"x = exp" will \_always\_ assign to a local variable. If you want to  
invoke the setter method you \_must\_ do "self.x = expr".

2. reading

"x" will access a local variable if it is defined and fall back to the  
getter method if there is no variable. "self.x" will always invoke  
the getter method regardless whether there is a local variable or not.

Example:

$ ruby19 -e 'def x;1;end;public :x;def f;p x, self.x;x=99;p x, self.x;end;f'  
1  
1  
99  
1

Needless to say that it's not the best of practices to mix local  
variables and properties of the same name. 🙂

Kind regards

robert

> **···**
>
> On Tue, Oct 4, 2011 at 5:52 PM, Dave Aronson \<rubytalk2dave@davearonson.com\> wrote:
> 
> > On Tue, Oct 4, 2011 at 10:03, Brian Candler \<b.candler@pobox.com\> wrote:
> > 
> > > (Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you  
> > > think it's clearer)
> > 
> > Not quite. This is one of Ruby's little gotchas. A naked "x" will be  
> > taken as a local variable in the method. If you want to use an  
> > instance variable you \*must\* precede it with either "self." or "@".
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Darryl\_L\_Pierce](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Darryl\_L\_Pierce](https://rubytalk.org/u/Darryl_L_Pierce)\
**Post date:** [4 October 2011 17:11 UTC](https://rubytalk.org/t/operator-overloading/63758/13 "2011-10-04T17:11:49Z")

</div>

Sorry, Brian was right. Try it out in the context of the code sample he  
posted to see for yourself.

> **···**
>
> On 10/04/2011 11:52 AM, Dave Aronson wrote:
> 
> > On Tue, Oct 4, 2011 at 10:03, Brian Candler \<b.candler@pobox.com\> wrote:
> > 
> > > (Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you  
> > > think it's clearer)
> > 
> > Not quite. This is one of Ruby's little gotchas. A naked "x" will be  
> > taken as a local variable in the method. If you want to use an  
> > instance variable you \*must\* precede it with either "self." or "@".
> 
> --  
> Darryl L. Pierce \<mcpierce@gmail.com\>  
> [http://mcpierce.multiply.com/](http://mcpierce.multiply.com/)  
> "What do you care what people think, Mr. Feynman?"

---

<div class="post-metadata">

**Author:** ![Darryl\_L\_Pierce](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Darryl\_L\_Pierce](https://rubytalk.org/u/Darryl_L_Pierce)\
**Post date:** [4 October 2011 15:31 UTC](https://rubytalk.org/t/operator-overloading/63758/14 "2011-10-04T15:31:39Z")

</div>

Okay, then this flies in the face of how I understood variables. I would  
have thought the act of having 'x' in the definition for bar in the  
above created a local variable in the current scope.

> **···**
>
> On 10/04/2011 11:11 AM, Adam Prescott wrote:
> 
> > > > (Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you  
> > > > think it's clearer)
> > > 
> > > I don't think so. 'x' is a local variable, while '@x' is an instance  
> > > variable. And since the fields are instance variables they have to be  
> > > prefixed with '@'.
> > 
> > Not quite. 🙂
> > 
> > class Foo  
> > &nbsp;&nbsp;attr\_reader :x
> > 
> > &nbsp;&nbsp;def initialize(x)  
> > &nbsp;&nbsp;&nbsp;&nbsp;@x = x  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def bar  
> > &nbsp;&nbsp;&nbsp;&nbsp;x # 🙂  
> > &nbsp;&nbsp;end  
> > end
> > 
> > f = Foo.new(1)  
> > puts f.bar
> > 
> > [http://ideone.com/mbPc0](http://ideone.com/mbPc0)
> > 
> > If there's no local variable in the scope, the interpreter sees it as a  
> > method call. If it's ambiguous, it'll pick the variable over the method  
> > call, unless you force it with a trailing ().
> > 
> > x = 1  
> > def x; 2; end  
> > puts x  
> > puts x()
> 
> --  
> Darryl L. Pierce \<mcpierce@gmail.com\>  
> [http://mcpierce.multiply.com/](http://mcpierce.multiply.com/)  
> "What do you care what people think, Mr. Feynman?"

---

<div class="post-metadata">

**Author:** ![Adam\_Prescott](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@Adam\_Prescott](https://rubytalk.org/u/Adam_Prescott)\
**Post date:** [4 October 2011 16:07 UTC](https://rubytalk.org/t/operator-overloading/63758/15 "2011-10-04T16:07:09Z")

</div>

> "x" will access a local variable if it is defined and fall back to the  
> getter method if there is no variable. "self.x" will always invoke  
> the getter method regardless whether there is a local variable or not.

x() too, as an alternative to self.x. Although I prefer the latter.

> Needless to say that it's not the best of practices to mix local  
> variables and properties of the same name. 🙂

+1!

> **···**
>
> On Tue, Oct 4, 2011 at 4:58 PM, Robert Klemme \<shortcutter@googlemail.com\>wrote:

---

<div class="post-metadata">

**Author:** ![Chris6](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Chris6](https://rubytalk.org/u/Chris6)\
**Post date:** [4 October 2011 15:42 UTC](https://rubytalk.org/t/operator-overloading/63758/16 "2011-10-04T15:42:00Z")

</div>

My understanding (probly flawed) is  
attr\_reader :x

generates the following code:

def x  
&nbsp;&nbsp;@x  
end

so in bar that 'x' is a call to the x method, which returns the '@x' value  
but doing it this way makes the x public readable, so should only be  
used if that is needed  
I'd stick with using @x

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [4 October 2011 15:51 UTC](https://rubytalk.org/t/operator-overloading/63758/17 "2011-10-04T15:51:03Z")

</div>

Only an assignment creates a local variable in the scope.

def bar  
&nbsp;&nbsp;x = 2  
end

This creates a local variable x in the method bar. If there are no  
assignments, the parser understands that references to x must be  
method calls. This is due to the syntax ambiguity between methods and  
local variables, because ruby allows you to call methods without  
parens. So, to break the ambiguity, the parser marks references as  
methods by default, and only as local variables when it sees an  
assignment.

Jesus.

> **···**
>
> On Tue, Oct 4, 2011 at 5:31 PM, Darryl L. Pierce \<mcpierce@gmail.com\> wrote:
> 
> > On 10/04/2011 11:11 AM, Adam Prescott wrote:
> > 
> > > > > (Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you  
> > > > > think it's clearer)
> > > > 
> > > > I don't think so. 'x' is a local variable, while '@x' is an instance  
> > > > variable. And since the fields are instance variables they have to be  
> > > > prefixed with '@'.
> > > 
> > > Not quite. 🙂
> > > 
> > > class Foo  
> > > attr\_reader :x
> > > 
> > > def initialize(x)  
> > > @x = x  
> > > end
> > > 
> > > def bar  
> > > x # 🙂  
> > > end  
> > > end
> > > 
> > > f = Foo.new(1)  
> > > puts f.bar
> > > 
> > > [http://ideone.com/mbPc0](http://ideone.com/mbPc0)
> > > 
> > > If there's no local variable in the scope, the interpreter sees it as a  
> > > method call. If it's ambiguous, it'll pick the variable over the method  
> > > call, unless you force it with a trailing ().
> > > 
> > > x = 1  
> > > def x; 2; end  
> > > puts x  
> > > puts x()
> > 
> > Okay, then this flies in the face of how I understood variables. I would  
> > have thought the act of having 'x' in the definition for bar in the  
> > above created a local variable in the current scope.

---

<div class="post-metadata">

**Author:** ![11142](https://avatars.discourse-cdn.com/v4/letter/1/e0b2c6/32.png) [@11142](https://rubytalk.org/u/11142)\
**Post date:** [4 October 2011 16:32 UTC](https://rubytalk.org/t/operator-overloading/63758/18 "2011-10-04T16:32:48Z")

</div>

This is actually only 1/7th true. There are as many as 7 ways to  
introduce a new variable, out of which anybody would guess about 5 if  
they though about it for a while ;), and 2 are quite obscure.

These are:

1. Single- or multiple-assignment (including sub-assignment) anywhere  
an expression is allowed  
2. A for loop's iterator variable(s)  
3. Formal arguments to a method  
4. Block arguments and block-local variables  
5. Stabby Lambda variables  
6. Rescue exception naming (rescue StandardError =\> err)  
7. Named Regexp captures in literal matches (/name: (?\<person\>\w+)/ =~  
'name: Mike' creates a local variable named "person" with value  
"Mike")

(The above borrowed from [https://github.com/michaeledgar/ripper-plus\](https://github.com/michaeledgar/ripper-plus%5C))

Ah, the joys of high-level programming 😃

-- Matma Rex

> **···**
>
> 2011/10/4 Jesús Gabriel y Galán \<jgabrielygalan@gmail.com\>:
> 
> > Only an assignment creates a local variable in the scope.

---

<div class="post-metadata">

**Author:** ![Darryl\_L\_Pierce](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Darryl\_L\_Pierce](https://rubytalk.org/u/Darryl_L_Pierce)\
**Post date:** [4 October 2011 17:10 UTC](https://rubytalk.org/t/operator-overloading/63758/19 "2011-10-04T17:10:18Z")

</div>

AH! That's what I missing, and what he meant by "the method". Okay. The  
OP though didn't have the attr\_accessor on the fields, though, that I  
remember.

> **···**
>
> On 10/04/2011 11:42 AM, Chris Hulan wrote:
> 
> > My understanding (probly flawed) is  
> > attr\_reader :x
> > 
> > generates the following code:
> > 
> > def x  
> > &nbsp;&nbsp;@x  
> > end
> > 
> > so in bar that 'x' is a call to the x method, which returns the '@x' value  
> > but doing it this way makes the x public readable, so should only be  
> > used if that is needed  
> > I'd stick with using @x
> 
> --  
> Darryl L. Pierce \<mcpierce@gmail.com\>  
> [http://mcpierce.multiply.com/](http://mcpierce.multiply.com/)  
> "What do you care what people think, Mr. Feynman?"

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [4 October 2011 20:30 UTC](https://rubytalk.org/t/operator-overloading/63758/20 "2011-10-04T20:30:09Z")

</div>

> > Only an assignment creates a local variable in the scope.
> 
> This is actually only 1/7th true. There are as many as 7 ways to  
> introduce a new variable, out of which anybody would guess about 5 if  
> they though about it for a while ;), and 2 are quite obscure.
> 
> These are:
> 
> 1. Single- or multiple-assignment (including sub-assignment) anywhere  
> an expression is allowed  
> 2. A for loop's iterator variable(s)

Kind of assignment 🙂

> 3. Formal arguments to a method

Good one, I was thinking only in the body of the method, but you can  
also argue that someone does an assignment to them 🙂

> 4. Block arguments and block-local variables

Block arguments and block local variables are local to the block  
scope, not the method.

> 5. Stabby Lambda variables

What do you mean by this?

> 6. Rescue exception naming (rescue StandardError =\> err)

Kind of assignment 🙂

> 7. Named Regexp captures in literal matches (/name: (?\<person\>\w+)/ =~  
> 'name: Mike' creates a local variable named "person" with value  
> "Mike")

Didn't knew this one, thanks.

Jesus.

> **···**
>
> 2011/10/4 Bartosz Dziewoński \<matma.rex@gmail.com\>:
> 
> > 2011/10/4 Jesús Gabriel y Galán \<jgabrielygalan@gmail.com\>:

[Next page](https://rubytalk.org/t/operator-overloading/63758.md?page=2)
