I\'m learning the basics of Ruby (just starting out), and I came across the Hash.[] method. It was introduced with
a = [\"foo\", 1, \"bar\", 2]
=> [\"foo\", 1
There are a couple methods that ruby lets you call in a special way. These are the []
as you mentioned, the +
, -
, ==
and the like as someone else mentioned. Another important example are methods of the form something=(value)
which can be called with object.something = value
and allow you to create accessors.
Edit:
Fun fact 1: if you define a +
method you get +=
for free.
Fun fact 2: if you define a <=>
you get all comparison methods, courtesy of Comparable