How to make sure a method returns an array, even when there is only one element in Ruby
Posted
by doctororange
on Stack Overflow
See other posts from Stack Overflow
or by doctororange
Published on 2010-03-19T15:21:17Z
Indexed on
2010/03/19
15:41 UTC
Read the original article
Hit count: 204
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.
© Stack Overflow or respective owner