Lesson 5: ' = ' between method name and parameters


#1

I'm having trouble understanding the function of the equals sign in the following method:

def name=(value)
@name = value
end

The text says that this is just Ruby convention, but I've experimented with it a bit, and it clearly changes the function of the code. With this short program:

greeting = "hello"
def a_method(value)
  value = value + " there."
  return value
end
puts a_method(greeting)

...I get "hello there," but if I do THIS...

greeting = "hello"
def a_method=(value)
  value = value + " there."
  return value
end
puts a_method=(greeting)

... I only get "hello". What is actually going on here? What does that ' = ' sign do? Thanks in advance!


#2

Hi its only change at the puts

puts a_method = greeting

greeting is equale to " hello "

So when you do that a_method = greeting
you dont call the function but you create a variable call a_method and save greeting So its like that.


a_method = "hello"

since greeting is equale to "hello".