I am really confused about the assignment method in Ruby. In the documentation, it says:
Methods that end with an equals sign indicate an assignment method. For assignment methods, the return value is ignored and the arguments are returned instead.
I know this can be use as setter method in Class. However, I still feel confused about the concept. In the code below:
class Foo
# setter method
def foo=(x)
puts "OK: x=#{x}"
end
end
f = Foo.new
f.foo = 123 # OK: x=123
def a=(value)
puts "value is #{value}"
end
a = 123 # why there is no output?
p a # 123, why this only return the argument?
p a = 123 # 123, why this only return the argument?
Why the method with equal sign run differently in the Class and outside the class?