So what does Array({a: :b}) return? I would suggest that it returns an array with a hash as the value: [{a: :b}] .
Your guess is wrong. Kernel#Array converts the arg argument, trying (in that order):
arg.to_aryarg.to_a- and finally creates
[arg]
<strong> Examples:
Array(1)
This is because of (3). There is no Fixnum#to_ary or Fixnum#to_a
Array([1, 2])
This does not return [[1, 2]] due to (1). Array#to_ary returns self
Array(nil) #=> []
This does not return [nil] due to (2). There is no NilClass#to_ary , but there is NilClass#to_a : "Always returns an empty array."
Array({a: :b})
Like Array(nil) , this does not return [{a: :b}] due to (2). There is no Hash#to_ary , but there Hash#to_a : "Converts hsh to a nested array of [key, value] arrays."
Array(Time.now) #=> [33, 42, 17, 22, 8, 2013, 4, 234, true, "CEST"]
This is Time#to_a return ... "ten-element array of time values"
Output:
Kernel#Array works as expected, Hash#to_a is the culprit. (or to_a in general)
I would like to have functionality that returns an array no matter what I insert.
Hash and Array are different objects. You can check the type ( Kernel#Array does this too):
def hash_to_array(h) h.is_a?(Array) ? h : [h] end
Or continue with Hash (or even Object ) and Array :
class Hash
See comments for alternative implementations.