Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在组中迭代哈希元素?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何在组中迭代哈希元素?

Ruby on rails 如何在组中迭代哈希元素?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个非常类似于以下JSON对象的哈希;这只是一个示例,而不是确切的输出 我需要迭代散列并按host或service\u name对其排序,然后在我使用的视图中输出反映这些排序的块 如果我进入视图,希望看到按主机列出的内容,并且我在该主机上获得了10个服务,那么我需要在视图上列出每个主机的服务。同样,当涉及到服务\u名称时 如果我有100个匹配的service\u名称,我需要将它们屏蔽并一起列出 我是Rails新手,通常会通过JavaScript和JSON来处理这个问题,因为所做的事情最终都是

我有一个非常类似于以下JSON对象的哈希;这只是一个示例,而不是确切的输出

我需要迭代散列并按
host
service\u name
对其排序,然后在我使用的视图中输出反映这些排序的块

如果我进入视图,希望看到按主机列出的内容,并且我在该主机上获得了10个服务,那么我需要在视图上列出每个主机的服务。同样,当涉及到
服务\u名称时

如果我有100个匹配的
service\u名称
,我需要将它们屏蔽并一起列出

我是Rails新手,通常会通过JavaScript和JSON来处理这个问题,因为所做的事情最终都是为大量的DOM操作而设计的,但负责人坚持这必须首先在服务器端完成,所以我不知所措

{
    "status": "successful",
    "service_list": [
        {
            "service_name": "oozie",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "single-namenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "single-database",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "datanode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "secondarynamenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "web",
            "status": "DEAD",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "tasktracker",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "jobtracker",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "oozie",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "single-namenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "single-database",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "datanode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "secondarynamenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "web",
            "status": "DEAD",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "tasktracker",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "jobtracker",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "oozie",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "single-namenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "single-database",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "datanode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "secondarynamenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "web",
            "status": "DEAD",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "tasktracker",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "jobtracker",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "oozie",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "single-namenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "single-database",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "datanode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "secondarynamenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "web",
            "status": "DEAD",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "tasktracker",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "jobtracker",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "oozie",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "single-namenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "single-database",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "datanode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "secondarynamenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "web",
            "status": "DEAD",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "tasktracker",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "jobtracker",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "oozie",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "single-namenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "single-database",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "datanode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "secondarynamenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "web",
            "status": "DEAD",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "tasktracker",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "jobtracker",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "oozie",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "single-namenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "single-database",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "datanode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "secondarynamenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "web",
            "status": "DEAD",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "tasktracker",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "jobtracker",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        }
    ]
}

下面是按
service\u name
和文本(非HTML)输出对它们进行分组的代码。HTML将是相同的,但具有合理的HTML标记。服务名称按字母顺序排序;没有理由,只是举个例子

svcs = val[:service_list]
puts "#{svcs.size} service entries."

grouped = svcs.group_by { |svc| svc[:service_name] }
puts "#{grouped.size} named services:"

grouped.keys.sort.each do |name|
    puts "  service: #{name}"
    grouped[name].each do |svc|
        puts "     host: #{svc[:host]}"
        puts "   status: #{svc[:status]}"
        puts "           #{svc[:status_message]}"
    end
    puts '-'*40
end
group\u by
返回数组的散列,在本例中由
service\u name
字符串键入(例如,
“web”
“tasktracker”
等),如果需要在每个组的信息中排序,请使用
排序
。例如,要按
:status
值在每个组内排序:

grouped.keys.sort.each do |name|
    puts "  service: #{name}"
    group_info = grouped[name]
    sorted_info = group_info.sort { |a, b| a[:status] <=> b[:status] }
    sorted_info.each do |svc|
        puts "     host: #{svc[:host]}"
        puts "   status: #{svc[:status]}"
        puts "           #{svc[:status_message]}"
    end
    puts '-'*40
end
使用和

#获取服务名称为“web”的所有内容 thingies.find_all{| thingy | thingy[:service_name]=“web”} #按状态对所有内容进行排序 thingies.sort_按{| thingy | thingy[:status]} #把它们放在一起 thingies.find_all{| thingy | thingy[:service_name]=“web”}\ .sort|u by{| thingy | thingy[:status]}
如果服务名称在系统对系统上不是动态的,那么这将在一定程度上起作用basis@chris什么意思?我写的代码只是为了说明方法本身。当然,在实际应用中,服务名称不会硬编码,而是来自其他地方。将来,将示例数据减少到演示问题所需的最小值。它帮助回答问题的人。
sort_by_status = Proc.new { |a, b| a[:status] <=> b[:status] }
sorted_info = group_info.sort &sort_by_status   
val = {
    :status => "successful",
    :service_list => [
        {
            :service_name => "oozie",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "single-namenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "single-database",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "datanode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "secondarynamenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "web",
            :status => "DEAD",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "tasktracker",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "jobtracker",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "oozie",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "single-namenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "single-database",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "datanode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "secondarynamenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "web",
            :status => "DEAD",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "tasktracker",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "jobtracker",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "oozie",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "single-namenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "single-database",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "datanode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "secondarynamenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "web",
            :status => "DEAD",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "tasktracker",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "jobtracker",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "oozie",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "single-namenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "single-database",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "datanode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "secondarynamenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "web",
            :status => "DEAD",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "tasktracker",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "jobtracker",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "oozie",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "single-namenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "single-database",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "datanode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "secondarynamenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "web",
            :status => "DEAD",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "tasktracker",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "jobtracker",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "oozie",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "single-namenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "single-database",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "datanode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "secondarynamenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "web",
            :status => "DEAD",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "tasktracker",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "jobtracker",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "oozie",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "single-namenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "single-database",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "datanode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "secondarynamenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "web",
            :status => "DEAD",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "tasktracker",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "jobtracker",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        }
    ]
}
# Get all the thingies with the service name "web" thingies.find_all {|thingy| thingy[:service_name] == 'web' } # Sort all the thingies by status thingies.sort_by {|thingy| thingy[:status] } # Put them together thingies.find_all {|thingy| thingy[:service_name] == 'web' } \ .sort_by {|thingy| thingy[:status]}