Let’s say I had a class like this:
class Parser
attr_accessor :config, :html
def initialize(config, html)
@config = config
@html = html
end
...
end
Is it safe to name the parameters to my initializer method the same as the attr_accessors? Is it bad style? What would be a better style?
It is totally safe to do this and I do it all the time. However, I think that’s it’s a better style to set your object attributes like this:
When you do this, your code will use the setter methods provided by attr_acessor. This way you always have a consistent way that your variables are accessed.