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.[](3)
  => 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 [](y)
   @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.

···

--
Posted via http://www.ruby-forum.com/.

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

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.

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

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

···

--
Posted via http://www.ruby-forum.com/\.

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?

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
  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 Sat, Apr 14, 2012 at 5:03 PM, Jan E. <lists@ruby-forum.com> wrote:

--
Posted via http://www.ruby-forum.com/\.

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

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

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:

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

···

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/

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

Not in the "normal" sense. However there is the RubySpec project:
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?

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`

# 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

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?"

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

Yes there is. In addition to the ruby-core mailing list (see
http://www.ruby-lang.org/en/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

···

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?"