如何在 Ruby 中迭代此哈希? [已关闭]

问题描述 投票:0回答:1
[:sha,"string"]
[:node_id,"dsd"]
[:stats,{:total=>122},:additions=>23],:deletions=>72}]
[:files,[{:sha=>"456"},:filename=>"456"],{:sha=>"123"},:filename=>"123"]]

我想访问文件 sha 和 stats 的内容。


  client.commit("healtheintent/hcc_reference_service","3a3c56babbc1c77323d178303fa06f8d11e1133d").each do |key,value|

    if("#{key}"=="files")
      puts ("#{value}")
    end
  end

现在值返回文件内的全部内容 同样,我如何访问统计数据

ruby iteration ruby-hash
1个回答
1
投票

一般如何迭代哈希

hash = { a: 1, b: 2, c: 2 }

hash.each do |key, value|
  if key == :a
    puts "a is #{value}"
  else
    puts "It is not a, it's #{key}"
  end
end

您还可以使用

each_key
each_value
方法仅迭代键或仅迭代值

© www.soinside.com 2019 - 2024. All rights reserved.