Search code examples
rubyattr-accessor

Function name followed by assignment operator


What does the following function definition mean? Why is there an assignment operator in the function name?

def func=(param)
  @param = param
end

Solution

  • What does the following function definition mean?

    They are called writer method in Ruby.

    Why there is an assignment operator in the function name?

    It adds sugar in your syntax.

    While you have a method as

    def func=(param)
      @param = param
    end
    

    You can call it as normal assignment

    ob.func = 12 # same as obj.func(12)