如何获得特定的输出迭代在Ruby中的哈希?(How to get a specific output

2019-06-18 00:13发布

我希望得到一个特定的输出迭代Ruby的哈希值。

这就是我要遍历哈希:

hash = {
  1 => ['a', 'b'], 
  2 => ['c'], 
  3 => ['d', 'e', 'f', 'g'], 
  4 => ['h']
}

这是我希望得到的输出:

1-----

a

b

2-----

c

3-----

d 

e

f

g

4-----

h

在Ruby中,我怎样才能得到我的哈希这样的输出?

Answer 1:

hash.each do |key, array|
  puts "#{key}-----"
  puts array
end

至于为了我要补充的是,在1.8的项目将会以随机顺序进行迭代(实际上由Fixnum对象的散列函数定义的顺序),而1.9将在字面的顺序进行迭代。



Answer 2:

The most basic way to iterate over a hash is as follows:

hash.each do |key, value|
  puts key
  puts value
end


Answer 3:

hash.keys.sort.each do |key|
  puts "#{key}-----"
  hash[key].each { |val| puts val }
end


Answer 4:

一个散列调用排序将其转换为嵌套数组,然后通过按键对其进行排序,所以你需要的是这样的:

puts h.sort.map {|k,v| ["#{k}----"] + v}

如果你实际上并不需要的“----”部分,它可以只是:

puts h.sort


Answer 5:

我的一条线的解决方案:

hash.each { |key, array| puts "#{key}-----", array }

我觉得这是很容易阅读。



Answer 6:

您还可以细化 Hash::each因此它将支持递归枚举。 这里是我的版本的Hash::eachHash::each_pair )与枚举支持:

module HashRecursive
    refine Hash do
        def each(recursive=false, &block)
            if recursive
                Enumerator.new do |yielder|
                    self.map do |key, value|
                        value.each(recursive=true).map{ |key_next, value_next| yielder << [[key, key_next].flatten, value_next] } if value.is_a?(Hash)
                        yielder << [[key], value]
                    end
                end.entries.each(&block)
            else
                super(&block)
            end
        end
        alias_method(:each_pair, :each)
    end
end

using HashRecursive

下面是使用的例子 Hash::each使用和不使用recursive标志:

hash = {
    :a => {
        :b => {
            :c => 1,
            :d => [2, 3, 4]
        },
        :e => 5
    },
    :f => 6
}

p hash.each, hash.each {}, hash.each.size
# #<Enumerator: {:a=>{:b=>{:c=>1, :d=>[2, 3, 4]}, :e=>5}, :f=>6}:each>
# {:a=>{:b=>{:c=>1, :d=>[2, 3, 4]}, :e=>5}, :f=>6}
# 2

p hash.each(true), hash.each(true) {}, hash.each(true).size
# #<Enumerator: [[[:a, :b, :c], 1], [[:a, :b, :d], [2, 3, 4]], [[:a, :b], {:c=>1, :d=>[2, 3, 4]}], [[:a, :e], 5], [[:a], {:b=>{:c=>1, :d=>[2, 3, 4]}, :e=>5}], [[:f], 6]]:each>
# [[[:a, :b, :c], 1], [[:a, :b, :d], [2, 3, 4]], [[:a, :b], {:c=>1, :d=>[2, 3, 4]}], [[:a, :e], 5], [[:a], {:b=>{:c=>1, :d=>[2, 3, 4]}, :e=>5}], [[:f], 6]]
# 6

hash.each do |key, value|
    puts "#{key} => #{value}"
end
# a => {:b=>{:c=>1, :d=>[2, 3, 4]}, :e=>5}
# f => 6

hash.each(true) do |key, value|
    puts "#{key} => #{value}"
end
# [:a, :b, :c] => 1
# [:a, :b, :d] => [2, 3, 4]
# [:a, :b] => {:c=>1, :d=>[2, 3, 4]}
# [:a, :e] => 5
# [:a] => {:b=>{:c=>1, :d=>[2, 3, 4]}, :e=>5}
# [:f] => 6

hash.each_pair(recursive=true) do |key, value|
    puts "#{key} => #{value}" unless value.is_a?(Hash)
end
# [:a, :b, :c] => 1
# [:a, :b, :d] => [2, 3, 4]
# [:a, :e] => 5
# [:f] => 6

下面是例如,从问题本身:

hash = {
    1   =>  ["a", "b"], 
    2   =>  ["c"], 
    3   =>  ["a", "d", "f", "g"], 
    4   =>  ["q"]
}

hash.each(recursive=false) do |key, value|
    puts "#{key} => #{value}"
end
# 1 => ["a", "b"]
# 2 => ["c"]
# 3 => ["a", "d", "f", "g"]
# 4 => ["q"]

也看看我的递归版本的Hash::mergeHash::merge! ) 在这里 。



文章来源: How to get a specific output iterating a hash in Ruby?
标签: ruby hash output