-
A splat on a hash converts it into an array.
[*{foo: :bar}] # => [[:foo, :bar]]Is there some hidden mechanism (such as implicit class cast) going on here, or is it a built-in primitive feature?
-
Besides an array, are
niland hash the only things that disappear/change with the splat operator under Ruby 1.9?
A splat on a hash converts it into an array. [*{foo: :bar}] # =>
Share
A splat will attempt an explicit conversion of an object to an Array.
To do this, it will send
to_aand expect anArrayas a result.If the object does not respond to
to_a, then there is no effect, e.g.[*42] == [42]Many builtin classes implement
to_a. In particular:Enumerable)ArrayHashRangeIOandFileEnumeratorEnumerator::Lazy(Ruby 2.0)SetandSortedSetNilClassMatchDataOpenStructStructTimeMatrixandVectorAll these can thus be splatted: