Do you need brackets in the setter? Or you can use var = str? Both variants will work, but for some reason everywhere there is a variant with brackets.
About the fact that you can use attr_writer do not say, so it’s understandable.

class SomeClass def var=(str) @var = str end def var @var end end 

    1 answer 1

    Ruby developers try to follow certain code design rules. You do not specify a semicolon at the end of the expression, although this is allowed. You do not specify a return in the last method expression, although this is allowed.

    Regarding parentheses framing method parameters, there is also a rule:

    Use def with parentheses when the method has parameters. Omit brackets when the method takes no parameters.

     # плохо def some_method() # некоторый код end # хорошо def some_method # некоторый код end # плохо def some_method_with_parameters param1, param2 # некоторый код end # хорошо def some_method_with_parameters(param1, param2) # некоторый код end 

    More details on the code design rules can be found here . This is why you see a setter with brackets everywhere — a sign of good Ruby code.

    • Good programmers adhere to the rules that are appropriate for the project, and not the harmful nonsense from Bbats. - Nakilon
    • @Nakilon Well, nevertheless, you can come to the project, which is screwed rubocop and your requests may simply not be considered if the rubocop tests did not pass on CI. This is an objective reality. And now guess what the rules are there and how often do they change them for the project? - cheops
    • Nafig-nafig such projects. - Nakilon