Jeff Moore wrote in post #1018257:
jack jones wrote in post #1018220:
Thank you very much Chuck Remes 
The problem is I don't understand those functions :
# now name just = ?? The problem is that I don't get the magic
10 def =(row, column, value) # how does it works?
11 position = index row, column
12 @array[position] = value
13 end
15 # retrieve value from row, column
16 def (row, column)
17 position = index row, column
18 return @array[position]
19 end
'=' is actually a method. Does that help?
More precisely, '=' is the name of the method. You can name a method
with three letters, right?
def abc(str)
puts str
end
This is the same:
def =(str)
puts str
end
The name of the first method is 'abc', and the name of the second method
is '='. Here is an example:
class Dog
def abc(str)
puts str
end
def =(str)
puts str
end
end
d = Dog.new
d.abc('hello')
d.=('world')
--output:--
hello
world
So in ruby, the = method is just a method with a funny looking name
that is hard to pronounce. However, in the case of = ruby lets you do
some special things; you can call = like this:
d[val1] = val2
and ruby translates that into:
d.=(val1, val2)
Here is a more complete example:
class Dog
attr_reader :attributes
def initialize
@attributes = {}
end
def abc(str)
puts str
end
def =(key, val)
@attributes[key] = val
end
end
d = Dog.new
d.abc('hello')
d['greeting'] = 'world'
p d.attributes
--output:--
hello
{"greeting"=>"world"}
However, ruby does some tricky things with the second method, so you
can call it like this:
···
--
Posted via http://www.ruby-forum.com/\.