I have a bunch of functions that take in either an array of objects, or a single object designed to be treated as an array containing one object, and I am looking for a cleaner way to accomplish this. Basically, I want to know how I could make the unless part in the following function more concise:
def foo(bar_or_bars)
unless bar_or_bars.is_a?(Array)
bar_or_bars = [bar_or_bars]
end
bar_or_bars.each { |baz| ... }
end
Any help will be appreciated! Thanks.
First thing you could do is to write the
unlesslogic in a single line:As you see, I give it a new name here, as it’s no longer a bar or bars, it’s now definitely a collection.
The problem with this and your original approach is that although your function could work on any
Enumerable, you will force your users to give you an argument of a specific type, which breaks duck typing.A neat trick to partially solve that issue is the following:
I wouldn’t exactly call that readable, though. It actually smells a lot like bad API design. Probably you should better take multiple arguments like this:
And let the caller decide whether he wants to pass a single object or an array: