Special character function: '=' in method name

Asked

Viewed 42 times

0

Good afternoon, everyone.

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

1 answer

0


The method containing the special character "=" indicates that that method is a Setter.

In Java, we define a Setter this way:

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

If you would like more information, please follow the documentation link on this subject: Ruby doc

Link to the same question in the international forum: What does the Equal ('=') Symbol do when put after the method name in a method Definition?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.