Ruby's Array and Array.wrap的区别

hash = {cgg: 11, cbq: 22}

 pry(main)> Array(hash)
[ [ :cgg, 11 ], [ :cbq, 22 ] ]

 pry(main)> Array.wrap(hash)
[ { :cgg => 11, :cbq => 22 } ]
Array(nil)
[]
Array(11)
=> [ 11 ]
Array([11])
=>[ 11 ]

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



ref: https://api.rubyonrails.org/classes/Array.html#method-c-wrap

Comments