# Question about op. overloading

**URL:** <https://rubytalk.org/t/question-about-op-overloading/50500>\
**Category:** ruby-talk\
**Created:** [3 December 2008 08:49 UTC](https://rubytalk.org/t/question-about-op-overloading/50500 "2008-12-03T08:49:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maarten\_Mortier](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Maarten\_Mortier](https://rubytalk.org/u/Maarten_Mortier)\
**Post date:** [3 December 2008 08:49 UTC](https://rubytalk.org/t/question-about-op-overloading/50500/1 "2008-12-03T08:49:28Z")

</div>

I have:

class Sudoku  
&nbsp;&nbsp;def[](r,c)  
&nbsp;&nbsp;&nbsp;&nbsp;@rows[r][c]  
&nbsp;&nbsp;end

&nbsp;&nbsp;def set(r,c,a)  
&nbsp;&nbsp;&nbsp;&nbsp;@rows[r][c]=a  
&nbsp;&nbsp;end  
end

Now, how can I define a []= operator instead of the awkward set(r,c,a) ?  
I want to be able to do  
sudoku[2,3]=candidate

or even  
sudoku[2][3] = candidate  
and  
sudoku[3] = row

Are these things possible? How, I could not find the information.

(I'm just learning, for educational reasons I don't want to extend the  
Matrix class)

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

---

<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:** [3 December 2008 09:01 UTC](https://rubytalk.org/t/question-about-op-overloading/50500/2 "2008-12-03T09:01:28Z")

</div>

A = operator is possible. It's syntactic sugar for a method named "="  
and at least two parameters: what's inside the brackets and the  
"assigned" value.

An example:

irb(main):001:0\> class A  
irb(main):002:1\> def = (a,b)  
irb(main):003:2\> @x[a] = b  
irb(main):004:2\> end  
irb(main):005:1\> attr\_reader :x  
irb(main):006:1\> def initialize  
irb(main):007:2\> @x = {}  
irb(main):008:2\> end  
irb(main):009:1\> end  
=\> nil  
irb(main):010:0\> a = A.new  
=\> #\<A:0xb7b2bfa8 @x={}\>  
irb(main):011:0\> a[:a] = :b  
=\> :b  
irb(main):012:0\> a  
=\> #\<A:0xb7b2bfa8 @x={:a=\>:b}\>

If you add more parameters to the method, you can use more "keys"  
inside the brackets:

irb(main):013:0\> class A  
irb(main):014:1\> def = (a,b,c)  
irb(main):015:2\> puts a,b,c  
irb(main):016:2\> end  
irb(main):017:1\> end  
=\> nil  
irb(main):018:0\> a = A.new  
=\> #\<A:0xb7b070f4 @x={}\>  
irb(main):019:0\> a[:a,:b] = :c  
a  
b  
c  
=\> :c

Hope this helps,

Jesus.

> **···**
>
> On Wed, Dec 3, 2008 at 9:49 AM, Maarten Mortier \<maarten.mortier@gmail.com\> wrote:
> 
> > I have:
> > 
> > class Sudoku  
> > def(r,c)  
> > &nbsp;&nbsp;&nbsp;@rows[r][c]  
> > end
> > 
> > def set(r,c,a)  
> > &nbsp;&nbsp;&nbsp;@rows[r][c]=a  
> > end  
> > end
> > 
> > Now, how can I define a = operator instead of the awkward set(r,c,a) ?  
> > I want to be able to do  
> > sudoku[2,3]=candidate
> > 
> > or even  
> > sudoku[2][3] = candidate  
> > and  
> > sudoku[3] = row
> > 
> > Are these things possible? How, I could not find the information.

---

<div class="post-metadata">

**Author:** ![Maarten\_Mortier](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Maarten\_Mortier](https://rubytalk.org/u/Maarten_Mortier)\
**Post date:** [3 December 2008 09:05 UTC](https://rubytalk.org/t/question-about-op-overloading/50500/3 "2008-12-03T09:05:04Z")

</div>

Jesús Gabriel y Galán wrote:

> Hope this helps,
> 
> Jesus.

So simple, I should have known. 🙂

Thank you!

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

---

<div class="post-metadata">

**Author:** ![Maarten\_Mortier](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Maarten\_Mortier](https://rubytalk.org/u/Maarten_Mortier)\
**Post date:** [3 December 2008 09:14 UTC](https://rubytalk.org/t/question-about-op-overloading/50500/4 "2008-12-03T09:14:35Z")

</div>

What about  
sudoku[r][c]= a  
though?

Is that possible? I thought I'd be able to fluke it by overloading [] to  
return an array. But how do I pass parameters inside the bracket?

And how woudl I get

puts sudoku[][3]

to return the third column?

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

---

<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:** [3 December 2008 10:20 UTC](https://rubytalk.org/t/question-about-op-overloading/50500/5 "2008-12-03T10:20:06Z")

</div>

> What about  
> sudoku[r][c]= a  
> though?
> 
> Is that possible? I thought I'd be able to fluke it by overloading to  
> return an array. But how do I pass parameters inside the bracket?

You do not need an array but rather a proxy:

class X  
&nbsp;&nbsp;ColProxy = Struct.new :owner, :column do  
&nbsp;&nbsp;&nbsp;&nbsp;def (row)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner.matrix[[column, row]]  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def =(row,val)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner.matrix[[column, row]] = val  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;attr\_reader :matrix

&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@matrix = {}  
&nbsp;&nbsp;end

&nbsp;&nbsp;def (column)  
&nbsp;&nbsp;&nbsp;&nbsp;ColProxy.new(self, column)  
&nbsp;&nbsp;end  
end

irb(main):026:0\> x = X.new  
=\> #\<X:0x7ff5443c @matrix={}\>  
irb(main):027:0\> x[1][2] = 3  
=\> 3  
irb(main):028:0\> x[1][2]  
=\> 3  
irb(main):029:0\> x  
=\> #\<X:0x7ff5443c @matrix={[1, 2]=\>3}\>

> And how woudl I get
> 
> puts sudoku[3]
> 
> to return the third column?

You can do it similarly. Just make the argument for X# optional and  
return another proxy.

I would really rather go with the other approach to stuff all  
coordinates in a single pair of brackets and or provide additional  
methods.

Kind regards

robert

> **···**
>
> 2008/12/3 Maarten Mortier \<maarten.mortier@gmail.com\>:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end

---

<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:** [3 December 2008 11:14 UTC](https://rubytalk.org/t/question-about-op-overloading/50500/6 "2008-12-03T11:14:37Z")

</div>

Maarten Mortier wrote:

> What about  
> sudoku[r][c]= a  
> though?
> 
> Is that possible?

Simpler would be if you use sudoku[r,c] = a

Just define your = method to take 3 arguments.

> And how woudl I get
> 
> puts sudoku[3]
> 
> to return the third column?

Again, your method can take 2 arguments. sudoku[nil,c] could be used  
to give the column.

But I think a separate method for this makes more sense: sudoku.col(c)

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

---

<div class="post-metadata">

**Author:** ![Maarten\_Mortier](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Maarten\_Mortier](https://rubytalk.org/u/Maarten_Mortier)\
**Post date:** [3 December 2008 11:17 UTC](https://rubytalk.org/t/question-about-op-overloading/50500/7 "2008-12-03T11:17:12Z")

</div>

Brian Candler wrote:

> But I think a separate method for this makes more sense: sudoku.col(c)

Yeah that's what I did eventually.

To be honest I could've found all this stuff myself, I was confused by  
the output ruby gave me and didn't realize I just needed an extra  
parameter.  
Sorry and thanks to everyone.

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