How to make sure a method returns an array, even when there is only one element in Ruby
- by doctororange
I have a Ruby method that searches an array of hashes and returns a subset of that array.
def last_actions(type = 'all')
actions = @actions
if type == 'run'
actions = actions.select {|a| a['type'] == "run" }
end
return actions
end
This works, except when there is only one action to return, in which case I don't think it is returning an array with one element, but just the element itself.
This becomes problematic later.
What's a good way to ensure it returns an array of 1 element in this case?
Thanks.