How does this work? def[](own_arg_1, own_arg_2)

Hi,

While reading some code, which tells that one can define a new
indexing operator, some of my neurons triggered a question. When
evaluating this code:

class Song
  def[](from_time, to_time)
    result = Song.new(self.title + " [extract]",
                 self.artist
                 to_time - from_time)
    result.set_start_time(from_time)
    result
  end
end

song[0,15].play

I was thinking: when one usually defines a method, the arguments are
placed after the method name, which is '[]' here, or so I thought.
That would be song[](0,15).play

How come it works 'just' by putting the argument between the brackets?

Krekna Mektek

Hi --

Hi,

While reading some code, which tells that one can define a new
indexing operator, some of my neurons triggered a question. When
evaluating this code:

class Song
def(from_time, to_time)
   result = Song.new(self.title + " [extract]",
                self.artist
                to_time - from_time)
   result.set_start_time(from_time)
   result
end
end

song[0,15].play

I was thinking: when one usually defines a method, the arguments are
placed after the method name, which is '' here, or so I thought.
That would be song(0,15).play

Actually the full method-call version would be:

   song.(0,15).play # note the first dot

How come it works 'just' by putting the argument between the brackets?

Because Matz is nice and gave us some nice syntactic sugar :slight_smile:

David

···

On Tue, 4 Oct 2005, Krekna Mektek wrote:

--
David A. Black
dblack@wobblini.net