# Operator overloading of the subscript operator

**URL:** <https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267>\
**Category:** ruby-talk\
**Created:** [14 April 2012 11:32 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267 "2012-04-14T11:32:36Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ralph\_Shnelvar](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@Ralph\_Shnelvar](https://rubytalk.org/u/Ralph_Shnelvar)\
**Post date:** [14 April 2012 11:32 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/1 "2012-04-14T11:32:36Z")

</div>

I understand that operators (e.g. +, -, \*, []) are nothing more than method names so that

&nbsp;&nbsp;irb(main):027:0\> x=[101,102,103,104,105]  
&nbsp;&nbsp;=\> [101, 102, 103, 104, 105]  
&nbsp;&nbsp;irb(main):028:0\> x.[](3)  
&nbsp;&nbsp;=\> 104

(Those familiar with operator overloading can skip the example and go to my question below. The example is for those who are not familiar with Ruby operator overloading.)

- - - -

I have stolen the example immediately below from

> **[Operator Overloading](http://strugglingwithruby.blogspot.com/2010/04/operator-overloading.html)**
>
> Ruby permits operator overloading, allowing you to define how operators will work with your own (or indeed any) classes. Here is a quick exa...

class Tester3  
def initialize ary  
&nbsp;&nbsp;&nbsp;@ary = ary  
end

def [](y)  
&nbsp;&nbsp;&nbsp;@ary[y]  
end

def []=(y, value)  
&nbsp;&nbsp;&nbsp;@ary[y] = value  
end

def \<\<(y)  
&nbsp;&nbsp;&nbsp;@ary \<\< y  
end

def +(y)  
&nbsp;&nbsp;&nbsp;@ary \<\< y  
end  
end

focusing on  
def []=(y, value)  
&nbsp;&nbsp;&nbsp;@ary[y] = value  
end

- - - - -

My question is a documentation one: Where in the Ruby docs would I find the semantics of the subscript method []= defined?

That is, how would I come to know that  
&nbsp;&nbsp;x.[]=(2, 'Tuesday')  
is equivalent to  
&nbsp;&nbsp;x[2]='Tuesday'

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [14 April 2012 15:03 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/2 "2012-04-14T15:03:29Z")

</div>

Hi,

You won't find this in the Ruby API, because it's a core feature of the  
language and not some special property of a method.

The Ruby interpreter evaluates every expression of the pattern

IDENTIFIER[VAL\_1, VAL\_2, ..., VAL\_n] = VAL

to a call of the []= method:

IDENTIFIER.[]=(VAL\_1, VAL\_2, ..., VAL\_n, VAL)

This should be explained in every good book about Ruby basics.

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

---

<div class="post-metadata">

**Author:** ![Ralph\_Shnelvar](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@Ralph\_Shnelvar](https://rubytalk.org/u/Ralph_Shnelvar)\
**Post date:** [14 April 2012 15:48 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/3 "2012-04-14T15:48:39Z")

</div>

Jan,

Saturday, April 14, 2012, 9:03:29 AM, you wrote:

> Hi,

> You won't find this in the Ruby API, because it's a core feature of the  
> language and not some special property of a method.

> The Ruby interpreter evaluates every expression of the pattern

> IDENTIFIER[VAL\_1, VAL\_2, ..., VAL\_n] = VAL

> to a call of the = method:

> IDENTIFIER.=(VAL\_1, VAL\_2, ..., VAL\_n, VAL)

> This should be explained in every good book about Ruby basics.

I have the Pickax book and I see two places operator overloading is mentioned: pages 130 and 345.

Isn't there official documentation about the Ruby core?

If one looks at page 333 which lists all the operators that are expressed as methods, one sees that subscript is the only one where the expression goes lexically inside the operator. As a special  
case shouldn't this be documented somewhere official?

Ralph

---

<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:** [16 April 2012 12:40 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/4 "2012-04-16T12:40:41Z")

</div>

Err, what?

$ ri19 -T Array#=  
Array#=

(from ruby core)

> **···**
>
> On Sat, Apr 14, 2012 at 5:03 PM, Jan E. \<lists@ruby-forum.com\> wrote:
> 
> > You won't find this in the Ruby API, because it's a core feature of the  
> > language and not some special property of a method.
> 
> ------------------------------------------------------------------------------  
> &nbsp;&nbsp;ary[index] = obj -\> obj  
> &nbsp;&nbsp;ary[start, length] = obj or other\_ary or nil -\> obj or other\_ary or nil  
> &nbsp;&nbsp;ary[range] = obj or other\_ary or nil -\> obj or other\_ary or nil
> 
> ------------------------------------------------------------------------------
> 
> Element Assignment---Sets the element at index, or replaces a subarray  
> starting at start and continuing for length elements, or  
> replaces a subarray specified by range. If indices are greater than  
> the current capacity of the array, the array grows automatically. A negative  
> indices will count backward from the end of the array. Inserts elements if  
> length is zero. An IndexError is raised if a negative index  
> points past the beginning of the array. See also Array#push, and  
> Array#unshift.
> 
> &nbsp;&nbsp;a = Array.new  
> &nbsp;&nbsp;a[4] = "4"; #=\> [nil, nil, nil, nil, "4"]  
> &nbsp;&nbsp;a[0, 3] = ['a', 'b', 'c'] #=\> ["a", "b", "c", nil, "4"]  
> &nbsp;&nbsp;a[1..2] = [1, 2] #=\> ["a", 1, 2, nil, "4"]  
> &nbsp;&nbsp;a[0, 2] = "?" #=\> ["?", 2, nil, "4"]  
> &nbsp;&nbsp;a[0..2] = "A" #=\> ["A", "4"]  
> &nbsp;&nbsp;a[-1] = "Z" #=\> ["A", "Z"]  
> &nbsp;&nbsp;a[1..-1] = nil #=\> ["A", nil]  
> &nbsp;&nbsp;a[1..-1] = #=\> ["A"]
> 
> > The Ruby interpreter evaluates every expression of the pattern
> > 
> > IDENTIFIER[VAL\_1, VAL\_2, ..., VAL\_n] = VAL
> > 
> > to a call of the = method:
> > 
> > IDENTIFIER.=(VAL\_1, VAL\_2, ..., VAL\_n, VAL)
> 
> Plus, the result of this expression is always VAL - regardless of the  
> return value of the method =.
> 
> > This should be explained in every good book about Ruby basics.
> 
> Yes. But this is only about the general syntax transformation.  
> Semantics of each implementation (e.g. Array#=, Hash#=) are part  
> of the class documentation (see above).
> 
> Kind regards
> 
> robert
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [14 April 2012 18:23 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/5 "2012-04-14T18:23:45Z")

</div>

Ralph Shnelvar wrote in post #1056506:

> Isn't there official documentation about the Ruby core?

Well, I think "official documentation" isn't the right word, because  
Ruby is an ever-changing community project and there are many different  
implementations (CRuby, JRuby, Rubinius etc.).

Personally, I think the Ruby book by Matz and Flanagan is a pretty good  
and complete reference.

There are also some formal specifications like for example this one:  
[http://www.ipa.go.jp/osc/english/ruby/Ruby\_final\_draft\_enu\_20100825.pdf](http://www.ipa.go.jp/osc/english/ruby/Ruby_final_draft_enu_20100825.pdf)

But of course they can never cover each and every aspect and be  
absolutely up to date.

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

---

<div class="post-metadata">

**Author:** ![Eric\_Christopherson](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/eric_christopherson/32/1849_2.png) [@Eric\_Christopherson](https://rubytalk.org/u/Eric_Christopherson)\
**Post date:** [15 April 2012 16:41 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/6 "2012-04-15T16:41:34Z")

</div>

There's also Kernel#`, which is called when you put something inside  
backticks or %x{}. Unlike and =, though, trying to override it in  
your own classes doesn't have any effect.

> **···**
>
> On Sat, Apr 14, 2012 at 10:48 AM, Ralph Shnelvar \<ralphs@dos32.com\> wrote:
> 
> > I have the Pickax book and I see two places operator overloading is mentioned: pages 130 and 345.
> > 
> > Isn't there official documentation about the Ruby core?
> > 
> > If one looks at page 333 which lists all the operators that are expressed as methods, one sees that subscript is the only one where the expression goes lexically inside the operator. As a special  
> > case shouldn't this be documented somewhere official?

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [16 April 2012 14:44 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/7 "2012-04-16T14:44:08Z")

</div>

Robert Klemme wrote in post #1056723:

> > You won't find this in the Ruby API, because it's a core feature of the  
> > language and not some special property of a method.
> 
> Err, what?
> 
> $ ri19 -T Array#=  
> Array#=

The OP asked about the assignment expression being interpreted as a  
method call:

Ralph Shnelvar wrote in post #1056479:

> That is, how would I come to know that  
> &nbsp;&nbsp;x.=(2, 'Tuesday')  
> is equivalent to  
> &nbsp;&nbsp;x[2]='Tuesday'

It wasn't about the built-in definitions of this method (which can of  
course be looked up at ruby-doc.org or wherever -- but I'm sure the OP  
already knew that).

> **···**
>
> > On Sat, Apr 14, 2012 at 5:03 PM, Jan E. \<lists@ruby-forum.com\> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ralph\_Shnelvar](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@Ralph\_Shnelvar](https://rubytalk.org/u/Ralph_Shnelvar)\
**Post date:** [14 April 2012 19:26 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/8 "2012-04-14T19:26:58Z")

</div>

Jan,

Saturday, April 14, 2012, 12:23:45 PM, you wrote:

> Ralph Shnelvar wrote in post #1056506:
> 
> > Isn't there official documentation about the Ruby core?

> Well, I think "official documentation" isn't the right word, because  
> Ruby is an ever-changing community project and there are many different  
> implementations (CRuby, JRuby, Rubinius etc.).

> Personally, I think the Ruby book by Matz and Flanagan is a pretty good  
> and complete reference.

> There are also some formal specifications like for example this one:  
> [http://www.ipa.go.jp/osc/english/ruby/Ruby\_final\_draft\_enu\_20100825.pdf](http://www.ipa.go.jp/osc/english/ruby/Ruby_final_draft_enu_20100825.pdf)

> But of course they can never cover each and every aspect and be  
> absolutely up to date.

So there is no generally agreed up standards committee?

Is there a document somewhere that describes each implementation and their differences?

Ralph

---

<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:** [15 April 2012 17:34 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/9 "2012-04-15T17:34:41Z")

</div>

W dniu 15 kwietnia 2012 18:41 użytkownik Eric Christopherson  
\<echristopherson@gmail.com\> napisał:

> > I have the Pickax book and I see two places operator overloading is mentioned: pages 130 and 345.
> > 
> > Isn't there official documentation about the Ruby core?
> > 
> > If one looks at page 333 which lists all the operators that are expressed as methods, one sees that subscript is the only one where the expression goes lexically inside the operator. As a special  
> > case shouldn't this be documented somewhere official?
> 
> There's also Kernel#`, which is called when you put something inside  
> backticks or %x{}. Unlike and =, though, trying to override it in  
> your own classes doesn't have any effect.

Uh, yes it does?

irb(main):001:0\> module A; def ` a; puts "backticks: #{a}"; end end  
=\> nil  
irb(main):002:0\> class B; include A; def asd; `ls`; end end  
=\> nil  
irb(main):003:0\> B.new.asd  
backticks: ls  
=\> nil  
irb(main):004:0\> include A  
=\> Object  
irb(main):005:0\> `ls`  
backticks: ls  
=\> nil

-- Matma Rex

> **···**
>
> > On Sat, Apr 14, 2012 at 10:48 AM, Ralph Shnelvar \<ralphs@dos32.com\> wrote:

---

<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:** [16 April 2012 20:25 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/10 "2012-04-16T20:25:45Z")

</div>

Hm, I would have understood it that way if the order was reversed, i.e.

That is, how would I come to know that  
&nbsp;&nbsp;x[2]='Tuesday'  
is equivalent to  
&nbsp;&nbsp;x.=(2, 'Tuesday')

because that would be the usual way of searching information, i.e. one  
sees x[2]='Tuesday' and wonders what it does. Anyway, apparently I  
misunderstood this part. Thanks for clarifying.

Kind regards

robert

> **···**
>
> On Mon, Apr 16, 2012 at 4:44 PM, Jan E. \<lists@ruby-forum.com\> wrote:
> 
> > Robert Klemme wrote in post #1056723:
> > 
> > > On Sat, Apr 14, 2012 at 5:03 PM, Jan E. \<lists@ruby-forum.com\> wrote:
> > > 
> > > > You won't find this in the Ruby API, because it's a core feature of the  
> > > > language and not some special property of a method.
> > > 
> > > Err, what?
> > > 
> > > $ ri19 -T Array#=  
> > > Array#=
> > 
> > The OP asked about the assignment expression being interpreted as a  
> > method call:
> > 
> > Ralph Shnelvar wrote in post #1056479:
> > 
> > > That is, how would I come to know that  
> > > x.=(2, 'Tuesday')  
> > > is equivalent to  
> > > x[2]='Tuesday'
> > 
> > It wasn't about the built-in definitions of this method (which can of  
> > course be looked up at ruby-doc.org or wherever -- but I'm sure the OP  
> > already knew that).
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [14 April 2012 21:33 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/11 "2012-04-14T21:33:58Z")

</div>

Ralph Shnelvar wrote in post #1056523:

> So there is no generally agreed up standards committee?

No. Why do you expect there to be one? Like I said, Ruby is a community  
project, there are no official committees behind it.

The most common implementation (CRuby) is lead by the inventor of the  
language, Matsumoto. So you may view his decisions as the de facto  
standard. But in fact every implementation may change features, leave  
them out or add new ones. No commitee will sue them for it.

> Is there a document somewhere that describes each implementation and  
> their differences?

Not that I know of. As a rule of thumb, you can view CRuby as the  
reference implementation. Other implementations may not be completely up  
to date or miss specific features (but they may also introduce  
interesting new features).

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

---

<div class="post-metadata">

**Author:** ![Ammar\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@Ammar\_Ali](https://rubytalk.org/u/Ammar_Ali)\
**Post date:** [14 April 2012 21:47 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/12 "2012-04-14T21:47:52Z")

</div>

Not in the "normal" sense. However there is the RubySpec project:  
[http://rubyspec.org/](http://rubyspec.org/)

Regards,  
Ammar

> **···**
>
> On Sat, Apr 14, 2012 at 10:26 PM, Ralph Shnelvar \<ralphs@dos32.com\> wrote:
> 
> > So there is no generally agreed up standards committee?

---

<div class="post-metadata">

**Author:** ![Eric\_Christopherson](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/eric_christopherson/32/1849_2.png) [@Eric\_Christopherson](https://rubytalk.org/u/Eric_Christopherson)\
**Post date:** [16 April 2012 05:02 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/13 "2012-04-16T05:02:29Z")

</div>

Oh, I see. Still, Ruby doesn't seem to convert foo`bar` into  
foo.`(bar) the way it converts foo[bar] into foo.(bar):

class Foo  
&nbsp;&nbsp;def `(x)  
&nbsp;&nbsp;&nbsp;&nbsp;puts "backticks around #{x}"  
&nbsp;&nbsp;end  
end

f = Foo.new  
f`bar`

# result:  
Errno::ENOENT: No such file or directory - bar

> **···**
>
> 2012/4/15 Bartosz Dziewoński \<matma.rex@gmail.com\>:
> 
> > W dniu 15 kwietnia 2012 18:41 użytkownik Eric Christopherson  
> > \<echristopherson@gmail.com\> napisał:
> > 
> > > On Sat, Apr 14, 2012 at 10:48 AM, Ralph Shnelvar \<ralphs@dos32.com\> wrote:
> > > 
> > > > I have the Pickax book and I see two places operator overloading is mentioned: pages 130 and 345.
> > > > 
> > > > Isn't there official documentation about the Ruby core?
> > > > 
> > > > If one looks at page 333 which lists all the operators that are expressed as methods, one sees that subscript is the only one where the expression goes lexically inside the operator. As a special  
> > > > case shouldn't this be documented somewhere official?
> > > 
> > > There's also Kernel#`, which is called when you put something inside  
> > > backticks or %x{}. Unlike and =, though, trying to override it in  
> > > your own classes doesn't have any effect.
> > 
> > Uh, yes it does?
> > 
> > irb(main):001:0\> module A; def ` a; puts "backticks: #{a}"; end end  
> > =\> nil  
> > irb(main):002:0\> class B; include A; def asd; `ls`; end end  
> > =\> nil  
> > irb(main):003:0\> B.new.asd  
> > backticks: ls  
> > =\> nil  
> > irb(main):004:0\> include A  
> > =\> Object  
> > irb(main):005:0\> `ls`  
> > backticks: ls  
> > =\> nil

---

<div class="post-metadata">

**Author:** ![Ralph\_Shnelvar](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@Ralph\_Shnelvar](https://rubytalk.org/u/Ralph_Shnelvar)\
**Post date:** [15 April 2012 01:58 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/14 "2012-04-15T01:58:34Z")

</div>

Jan,

Saturday, April 14, 2012, 3:33:58 PM, you wrote:

> Ralph Shnelvar wrote in post #1056523:
> 
> > So there is no generally agreed up standards committee?

> No. Why do you expect there to be one? Like I said, Ruby is a community  
> project, there are no official committees behind it.

SO is there an unofficial chat list where I can say something like "I'd like to see X added to the language and I am willing to code it ... but before I do, do you guys think this is something you might  
like to add?"

---

<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:** [16 April 2012 17:16 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/15 "2012-04-16T17:16:18Z")

</div>

W dniu 16 kwietnia 2012 07:02 użytkownik Eric Christopherson  
\<echristopherson@gmail.com\> napisał:

> Oh, I see. Still, Ruby doesn't seem to convert foo`bar` into  
> foo.`(bar) the way it converts foo[bar] into foo.(bar):
> 
> class Foo  
> def `(x)  
> puts "backticks around #{x}"  
> end  
> end
> 
> f = Foo.new  
> f`bar`

Well, yes.

f`bar` is the same as f(`bar`).

`bar` is the same as self.`("bar"). You can't change the self here.

-- Matma Rex

---

<div class="post-metadata">

**Author:** ![Ammar\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@Ammar\_Ali](https://rubytalk.org/u/Ammar_Ali)\
**Post date:** [15 April 2012 05:20 UTC](https://rubytalk.org/t/operator-overloading-of-the-subscript-operator/65267/16 "2012-04-15T05:20:43Z")

</div>

Yes there is. In addition to the ruby-core mailing list (see  
[http://www.ruby-lang.org/en/community/\](http://www.ruby-lang.org/en/community/%5C)) there is also ruby's redmine  
instance ([http://bugs.ruby-lang.org/\](http://bugs.ruby-lang.org/%5C)) where one can post feature  
requests, patches, and discuss them.

Regards,  
Ammar

> **···**
>
> On Sun, Apr 15, 2012 at 4:58 AM, Ralph Shnelvar \<ralphs@dos32.com\> wrote:
> 
> > SO is there an unofficial chat list where I can say something like "I'd like to see X added to the language and I am willing to code it ... but before I do, do you guys think this is something you might  
> > like to add?"
