From: Robert Klemme Date: 2012-04-16T21:40:41+09:00 Subject: Re: Operator overloading of the subscript operator On Sat, Apr 14, 2012 at 5:03 PM, Jan E. 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/