Operator overloading of the subscript operator

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

irb(main):027:0> x=[101,102,103,104,105]
=> [101, 102, 103, 104, 105]
irb(main):028:0> x.
=> 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

class Tester3
def initialize ary
@ary = ary
end

def
@ary[y]
end

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

def <<(y)
@ary << y
end

def +(y)
@ary << y
end
end

focusing on
def []=(y, value)
@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
x.[]=(2, ‘Tuesday’)
is equivalent to
x[2]=‘Tuesday’

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.

Jan,

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

JE> Hi,

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

JE> The Ruby interpreter evaluates every expression of the pattern

JE> IDENTIFIER[VAL_1, VAL_2, …, VAL_n] = VAL

JE> to a call of the []= method:

JE> IDENTIFIER.[]=(VAL_1, VAL_2, …, VAL_n, VAL)

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

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

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

Jan,

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

JE> Ralph S. wrote in post #1056506:

Isn’t there official documentation about the Ruby core?

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

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

JE> There are also some formal specifications like for example this one:
JE>
http://www.ipa.go.jp/osc/english/ruby/Ruby_final_draft_enu_20100825.pdf

JE> But of course they can never cover each and every aspect and be
JE> 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

On Sat, Apr 14, 2012 at 10:26 PM, Ralph S. [email protected]
wrote:

So there is no generally agreed up standards committee?

Not in the “normal” sense. However there is the RubySpec project:
http://rubyspec.org/

Regards,
Ammar

Ralph S. 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).

Jan,

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

JE> Ralph S. wrote in post #1056523:

So there is no generally agreed up standards committee?

JE> No. Why do you expect there to be one? Like I said, Ruby is a
community
JE> 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?”

On Sat, Apr 14, 2012 at 10:48 AM, Ralph S. [email protected]
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.

On Sun, Apr 15, 2012 at 4:58 AM, Ralph S. [email protected]
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?”

Yes there is. In addition to the ruby-core mailing list (see
Community) there is also ruby’s redmine
instance (http://bugs.ruby-lang.org/) where one can post feature
requests, patches, and discuss them.

Regards,
Ammar

W dniu 15 kwietnia 2012 18:41 użytkownik Eric C.
[email protected] napisał:

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

2012/4/15 Bartosz D. [email protected]:

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

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

class Foo
def `(x)
puts “backticks around #{x}”
end
end

f = Foo.new
fbar

result:

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

On Sat, Apr 14, 2012 at 5:03 PM, Jan E. [email protected] 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#[]=

(from ruby core)

ary[index] = obj → obj
ary[start, length] = obj or other_ary or nil → obj or other_ary or
nil
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.

a = Array.new
a[4] = “4”; #=> [nil, nil, nil, nil, “4”]
a[0, 3] = [ ‘a’, ‘b’, ‘c’ ] #=> [“a”, “b”, “c”, nil, “4”]
a[1…2] = [ 1, 2 ] #=> [“a”, 1, 2, nil, “4”]
a[0, 2] = “?” #=> [“?”, 2, nil, “4”]
a[0…2] = “A” #=> [“A”, “4”]
a[-1] = “Z” #=> [“A”, “Z”]
a[1…-1] = nil #=> [“A”, nil]
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

W dniu 16 kwietnia 2012 07:02 użytkownik Eric C.
[email protected] napisał:

fbar
Well, yes.

fbar is the same as f(bar).

bar is the same as self.`(“bar”). You can’t change the self here.

– Matma R.

Robert K. wrote in post #1056723:

On Sat, Apr 14, 2012 at 5:03 PM, Jan E. [email protected] 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 S. 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).

On Mon, Apr 16, 2012 at 4:44 PM, Jan E. [email protected] wrote:

course be looked up at ruby-doc.org or wherever – but I’m sure the OP
already knew that).

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

That is, how would I come to know that
x[2]=‘Tuesday’
is equivalent to
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