迭代哈希数组并在红宝石下拉列表中显示

时间:2013-08-29 10:22:43

标签: ruby arrays loops hashmap traversal

我的哈希数组是下面的一个。

[{"status"=>"Submitted", "reachable"=>false}, 
{"status"=>"Replied", "reachable"=>false}, 
{"status"=>"Answered", "reachable"=>false}, 
{"status"=>"Assigned", "reachable"=>false}, 
{"status"=>"Started", "reachable"=>true}, 
{"status"=>"Closed", "reachable"=>false}, 
{"status"=>"Canceled", "reachable"=>true}]

我需要" status"值并在带有嵌入式ruby代码的html下拉列表中显示它们。你们中的任何人都可以让我知道示例代码吗? 我对ruby很新,并且在迭代中混淆并在下拉列表中显示值。

1 个答案:

答案 0 :(得分:3)

是,如下:

array = [{"status"=>"Submitted", "reachable"=>false}, 
        {"status"=>"Replied", "reachable"=>false}, 
        {"status"=>"Answered", "reachable"=>false}, 
        {"status"=>"Assigned", "reachable"=>false}, 
        {"status"=>"Started", "reachable"=>true}, 
        {"status"=>"Closed", "reachable"=>false}, 
        {"status"=>"Canceled", "reachable"=>true}]

p array.map{|h| h["status"] }
# >> ["Submitted", "Replied", "Answered", "Assigned", "Started", "Closed", "Canceled"]