Trying to get the values out of a collection that could be either an Array or a Hash, but switching based on type seems awkward:
def values_from_collection(array_or_hash)
case array_or_hash
when array_or_hash.is_a? Array
array_or_hash
when array_or_hash.is_a? Hash
array_or_hash.values
end
end
Is seems like there should be a single interface/method that both classes support, but nothing obvious stands out in Enumerable. Is there a standard way to accomplish this?
Well, it doesn’t seem so to me. Hash and Array are very different data structures. Why do you think they should support this?
Anyway, you could, for example, monkey-patch Array class to add
valuesmethodAnd then your method is greatly simplified:
But this is worse than branching, in my opinion.