149

Given an array, a single element, or nil, obtain an array - the latter two being a single element array and an empty array respectively.

I mistakenly figured Ruby would work this way:

[1,2,3].to_a  #= [1,2,3]     # Already an array, so no change
1.to_a        #= [1]         # Creates an array and adds element
nil.to_a      #= []          # Creates empty array

But what you really get is:

[1,2,3].to_a  #= [1,2,3]         # Hooray
1.to_a        #= NoMethodError   # Do not want
nil.to_a      #= []              # Hooray

So to solve this, I either need to use another method, or I could meta program by modifying the to_a method of all classes I intend to use - which is not an option for me.

So a Method it is:

result = nums.class == "Array".constantize ? nums : (nums.class == "NilClass".constantize ? [] : ([]<<nums))

The problem is that it is a bit of a mess. Is there an elegant way of doing this? (I would be amazed if this is the Ruby-ish way to solve this problem)


What applications does this have? Why even convert to an array?

In Rails' ActiveRecord, calling say, user.posts will either return an array of posts, a single post, or nil. When writing methods which work on the results of this, it is easiest to assume that the method will take an array, which may have zero, one, or many elements. Example method:

current_user.posts.inject(true) {|result, element| result and (element.some_boolean_condition)}
7

10 Answers 10

175

[*foo] or Array(foo) will work most of the time, but for some cases like a hash, it messes it up.

Array([1, 2, 3])    # => [1, 2, 3]
Array(1)            # => [1]
Array(nil)          # => []
Array({a: 1, b: 2}) # => [[:a, 1], [:b, 2]]

[*[1, 2, 3]]    # => [1, 2, 3]
[*1]            # => [1]
[*nil]          # => []
[*{a: 1, b: 2}] # => [[:a, 1], [:b, 2]]

The only way I can think of that works even for a hash is to define a method.

class Object; def ensure_array; [self] end end
class Array; def ensure_array; to_a end end
class NilClass; def ensure_array; to_a end end

[1, 2, 3].ensure_array    # => [1, 2, 3]
1.ensure_array            # => [1]
nil.ensure_array          # => []
{a: 1, b: 2}.ensure_array # => [{a: 1, b: 2}]
12
  • 2
    instead of ensure_array, extend to_a
    – Dan Grahn
    Aug 21, 2013 at 13:50
  • 10
    @screenmutt That would affect methods that rely on the original use of to_a. For instance, {a: 1, b: 2}.each ... would work differently.
    – sawa
    Aug 21, 2013 at 14:15
  • 1
    Can you explain this syntax? In many years of Ruby i had never come across this type of invocation. What do parentheses on a class name do? I can't find this in docs. Oct 27, 2015 at 21:08
  • 1
    @mastaBlasta Array(arg) tries to create a new array by calling to_ary, then to_a on the argument. This is documented in official ruby docs. I learned about it from Avdi's "Confident Ruby" book.
    – mambo
    Jul 27, 2016 at 15:41
  • 4
    @mambo At some point after i had posted my question I did find the answer. The hard part was that it has nothing to do with Array class but it's a method on Kernel module. ruby-doc.org/core-2.3.1/Kernel.html#method-i-Array Aug 2, 2016 at 14:38
146

With ActiveSupport (Rails): Array.wrap

Array.wrap([1, 2, 3])     # => [1, 2, 3]
Array.wrap(1)             # => [1]
Array.wrap(nil)           # => []
Array.wrap({a: 1, b: 2})  # => [{:a=>1, :b=>2}]

If you are not using Rails, you can define your own method similar to the rails source.

class Array
  def self.wrap(object)
    if object.nil?
      []
    elsif object.respond_to?(:to_ary)
      object.to_ary || [object]
    else
      [object]
    end
  end
end
1
  • 14
    class Array; singleton_class.send(:alias_method, :hug, :wrap); end for extra cuteness.
    – rthbound
    Sep 17, 2015 at 16:10
21

The simplest solution is to use [foo].flatten(1). Unlike other proposed solutions, it will work well for (nested) arrays, hashes and nil:

def wrap(foo)
  [foo].flatten(1)
end

wrap([1,2,3])         #= [1,2,3]
wrap([[1,2],[3,4]])   #= [[1,2],[3,4]]
wrap(1)               #= [1]
wrap(nil)             #= [nil]
wrap({key: 'value'})  #= [{key: 'value'}]
1
  • unfortunately this one has serious performance issue compared to other approaches. Kernel#Array i.e. Array() is the fastest of them all. Ruby 2.5.1 comparison: Array(): 7936825.7 i/s. Array.wrap: 4199036.2 i/s - 1.89x slower. wrap: 644030.4 i/s - 12.32x slower Sep 20, 2018 at 19:18
20

Array(whatever) should do the trick

Array([1,2,3]) # [1,2,3]
Array(nil) # []
Array(1337)   # [1337]
1
  • 14
    won't work for Hash. Array({a: 1, b: 2}) will be [[:a, 1], [:b, 2]]
    – davispuh
    May 9, 2014 at 10:58
13

ActiveSupport (Rails)

ActiveSupport has a pretty nice method for this. It's loaded with Rails, so defiantly the nicest way to do this:

Array.wrap([1, 2, 3]) #=> [1, 2, 3]
Array.wrap(nil) #=> nil

Splat (Ruby 1.9+)

The splat operator (*) un-arrays an array, if it can:

*[1,2,3] #=> 1, 2, 3 (notice how this DOES not have braces)

Of course, without an array, it does weird things, and the objects you "splat" need to be put in arrays. It's somewhat weird, but it means:

[*[1,2,3]] #=> [1, 2, 3]
[*5] #=> [5]
[*nil] #=> []
[*{meh: "meh"}] #=> [[:meh, "meh"], [:meh2, "lol"]]

If you don't have ActiveSupport, you can define the method:

class Array
    def self.wrap(object)
        [*object]
    end
end

Array.wrap([1, 2, 3]) #=> [1, 2, 3]
Array.wrap(nil) #=> nil

Although, if you plan on having large arrays, and less non-array things, you might want to change it - the above method is slow with large arrays, and can even cause your Stack to Overflow (omg so meta). Anyways, you might want to do this instead:

class Array
    def self.wrap(object)
        object.is_a? Array ? object : [*object]
    end
end

Array.wrap([1, 2, 3]) #=> [1, 2, 3]
Array.wrap(nil) #=> [nil]

I also have some benchmarks with and without the teneray operator.

5
  • Won't work for big arrays. SystemStackError: stack level too deep for 1M elements (ruby 2.2.3). Nov 29, 2015 at 3:19
  • @denis.peplin seems like you got a StackOverflow error :D - honestly, I'm not sure what happened. Sorry.
    – Ben Aubin
    Nov 29, 2015 at 5:06
  • I recently tried Hash#values_at with 1M arguments (using splat), and it throws the same error. Nov 29, 2015 at 18:17
  • @denis.peplin Does it work with object.is_a? Array ? object : [*object]?
    – Ben Aubin
    Feb 18, 2016 at 17:37
  • 1
    Array.wrap(nil) returns [] not nil :/
    – Aeramor
    Feb 24, 2016 at 22:30
7

How about

[].push(anything).flatten
2
  • 2
    Yeah I think I ended up using [anything].flatten in my case... but for the general case this will also flatten any nested array structures
    – xxjjnn
    Nov 10, 2014 at 10:45
  • 2
    [].push(anything).flatten(1) would work! It does not flatten nested arrays!
    – xxjjnn
    Apr 21, 2016 at 10:45
2

With the risk of stating the obvious, and knowing that this isn't the most tasty syntactic sugar ever seen on the planet and surrounding areas, this code seems to do exactly what you describe:

foo = foo.is_a?(Array) ? foo : foo.nil? ? [] : [foo]
1

you can overwrite the array method of Object

class Object
    def to_a
        [self]
    end
end

everything inherits Object, therefore to_a will now be defined for everything under the sun

1
  • 4
    blasphemous monkey patching! Repent ye!
    – xxjjnn
    Feb 8, 2016 at 16:26
1

I've gone through all the answers and mostly don't work in ruby 2+

But elado has the most elegant solution i.e

With ActiveSupport (Rails): Array.wrap

Array.wrap([1, 2, 3]) # => [1, 2, 3]

Array.wrap(1) # => [1]

Array.wrap(nil) # => []

Array.wrap({a: 1, b: 2}) # => [{:a=>1, :b=>2}]

Sadly but this also doesn't work for ruby 2+ as you will get an error

undefined method `wrap' for Array:Class

So in order to fix that you need to require.

require 'active_support/deprecation'

require 'active_support/core_ext/array/wrap'

0

Since the method #to_a already exists for the two main problematic classes (Nil and Hash), just define a method for the rest by extending Object:

class Object
    def to_a
        [self]
    end
end

and then you can easily call that method on any object:

"Hello world".to_a
# => ["Hello world"]
123.to_a
# => [123]
{a:1, b:2}.to_a
# => [[:a, 1], [:b, 2]] 
nil.to_a
# => []
1
  • 6
    I really think monkey patching a core Ruby class, especially object, is to be avoided. I'll give ActiveSupport a pass though so consider me a hypocrite. The solutions above by @sawa are much more viable than this. Jul 7, 2014 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.