Special character function: '=' in the method name

0

Good afternoon guys.

I know Ruby accepts three special characters in the method name: '!', '?' and '='. I need to know what the "=" indicates and what it does.

    
asked by anonymous 25.08.2017 / 19:53

1 answer

0

The method that contains the special character "=" indicates that that method is a setter.

In Java, we define a setter like this:

public class Welcome{
    private int number;

    public void setNumber(int number){
        this.number = number;
    }
}

In Ruby, a setter is defined this way:

class Welcome
  def hello=(mymessage)
    @message = mymessage
  end
end

For more information, see the documentation link on this subject: Ruby doc

Link to the same question in the international forum: What does the ('=') symbol mean when put the method name in a method definition?

    
28.08.2017 / 13:40