Ruby on rails 组织符号和散列的数组

Ruby on rails 组织符号和散列的数组,ruby-on-rails,arrays,ruby,Ruby On Rails,Arrays,Ruby,有没有更有效的方法 items = [:one, :two, {three: 3}, {four: 4}, :five, :six] # => [:one, :two, {:three=>3}, {:four=>4}, :five, :six] symbols = items.select { |n| n.class == Symbol } # => [:one, :two, :five, :six] params = items.selec

有没有更有效的方法

items = [:one, :two, {three: 3}, {four: 4}, :five, :six]  # => [:one, :two, {:three=>3}, {:four=>4}, :five, :six]

symbols = items.select { |n| n.class == Symbol }              # => [:one, :two, :five, :six]
params = items.select { |n| n.class == Hash }.reduce(:merge)  # => {:three=>3, :four=>4}
symbols << params                                             # => [:one, :two, :five, :six, {:three=>3, :four=>4}]
items=[:一,:二,{3:3},{4:4},:五,:六]#=>[:一,:二,{:三=>3},{:四=>4},:五,:六]
符号=项目。选择{n | n.class==Symbol}#=>[:一、二、五、六]
params=items.select{n}n.class==Hash}.reduce(:merge){three=>3,:four=>4}
符号[:一,:二,:五,:六,{:三=>3,:四=>4}]

如果数组中只有符号和散列,则可以使用,并通过两个步骤获得相同的结果:

s, h = items.partition {|i| i.class == Symbol}
s << h.reduce(&:merge)
#=> [:one, :two, :five, :six, {:three=>3, :four=>4}]
s,h=items.partition{| i | i.class==Symbol}
s[:一,:二,:五,:六,{:三=>3,:四=>4}]

还有什么别的吗?有没有办法只组织符号和散列?user2012677,在这种情况下,write
h,s=items.partition{i | i.class==Hash}
(第二行没有更改)。