I have an initialize method for a class that looks like this:
def initialize(
site:,
name:,
date_online: '-',
date_offline: '-',
date_modified: '-',
date_tagged: '-',
tags: []
)
@site = site
@name = name
@date_online = date_online
@date_offline = date_offline
@date_modified = date_modified
@date_tagged = date_tagged
@tags = tags
end
However, RuboCop is telling me that I should:
Metrics/ParameterLists: Avoid parameter lists longer than 5 parameters. [7/5]
So, what is the best way to rectify this? It's an initializer, so all those parameters are necessary. Is the desired solution to use a hash?
def initialize(args)
@site = args[:site]
@name = args[:name]
@date_online = args[:date_online] || '-'
@date_offline = args[:date_offline] || '-'
@date_modified = args[:date_modified] || '-'
@date_tagged = args[:date_tagged] || '-'
@tags = args[:tags] || []
end
Then how should I manually handle the fact that site
and name
are required? I could add some code after the assignments that will throw an error if the required fields aren't present:
raise ArgumentError unless @site && @name
That makes sense to me. But I assume the makers of RuboCop know what they're talking about, which leads me to suspect that there's a standard for this sort of thing.