Note: In Rails 3+ you can use
Array.wrap
Show archive.org snapshot
instead of the solution here.
In the past you could use Array(...)
or #to_a
to turn an object into an array, unless it already is an array:
Array(5) # => [5]
Array([5]) # => [5]
Unfortunately this idiom has issues and it also triggers deprecation warnings like this one:
warning: default `to_a' will be obsolete
For an alternative you can copy the attached file to config/initializers
or require it from a non-Rails project. You can now say:
5.listify # => [5]
[5].listify # => [5]
Note that this method no longer treats nil
receivers as a special case:
Array(nil) # => []
nil.listify # => [nil]
Posted by Henning Koch to makandra dev (2011-06-06 12:32)