哈希内的Rails循环

时间:2018-07-17 21:52:13

标签: ruby-on-rails ruby

这应该很简单,但事实并非如此 我正在尝试生成一个带有循环的json。像这样:

%do

我想得到:

%let years = 1989 1990 1995
%local i next_year
%do i=1 %to %sysfunc(%countw(&years));
    next_year = %scan(&years, &i);
    %macro1(&next_year);
%end

尝试使用地图,尝试了很多事情。只是遇到错误或将数组抛出给我。

编辑:我将提供我需要产生的确切json结构,以使其更加清晰。

更新

使用Damian的建议,我非常接近使它起作用,但是我只设法返回数组中的最后一个结果。前两个可能被all_urls = ["example.com/homepage", "example.com/another", "example.com"] hash = { "type":"URLS", "Params":{ "rules":{ "condition":"AND", "rules":[ begin all_urls.each do |url| { "condition":"AND", "rules":[ { "value":[ "data", "^https{0,1}:\\/\\/#{url}\\/{0,1}$" ] }, { "id":"@timestamp.age", } ] } end end ] } } } 覆盖了吗?见下文:

    {
        "type":"URLS",
        "Params":{
            "rules":{
                "condition":"AND",
                "rules":[
                    {
                        "condition":"AND",
                        "rules":[
                            {
                                "value":[
                                    "data",
                                    "^https{0,1}:\\/\\/example.com/homepage\\/{0,1}$"
                                ]
                            },
                            {
                                "id":"@timestamp.age",
                            }
                        ]
                    },
                    {
                        "condition":"AND",
                        "rules":[
                            {
                                "value":[
                                    "data",
                                    "^https{0,1}:\\/\\/example.com/another\\/{0,1}$"
                                ]
                            },
                            {
                                "id":"@timestamp.age",
                            }
                        ]
                    },
                    {
                        "condition":"AND",
                        "rules":[
                            {
                                "value":[
                                    "data",
                                    "^https{0,1}:\\/\\/example.com\\/{0,1}$"
                                ]
                            },
                            {
                                "id":"@timestamp.age",
                            }
                        ]
                    }
                ]
            }
        }
    }

我得到:

merge!

更新3

似乎我的主要错误是假设调用中的“规则”始终是数组,而实际上,它有时是数组,有时是对象。因此,我感到困惑。

以下两个答案都是很好的解决方案,我最终使用Damian的哈希值注入在循环内生成哈希值,这是使其工作的关键。

我最终要做的是创建“ Damian”数组,然后使用a = ["example.com/homepage", "example.com/another", "example.com"] hash = { "type":"condition", "Params":{ "rules":{ "condition":"AND", "rules": ( a.inject(Hash.new) do |hash_container, element| hash_container.merge!({ "condition":"AND", "rules":[ { "value":[ "^https{0,1}:\\/\\/#{element}\\/{0,1}$" ] }, ] }) end ) } } } 在该数组之前添加更多对象,最后在调用中添加该数组。

2 个答案:

答案 0 :(得分:2)

编辑符合条件的问题

您可以在哈希内执行函数而不会出现问题,我认为问题在于您如何使用哈希和函数。看(我会用reduce代替foreach,因为我更喜欢它,但是是一样的):

all_urls = ["example.com/homepage", "example.com/another", "example.com"]
hash = {
    "type":"condition",
    "Params":{
        "rules":{
            "condition":"AND",
            "rules":
            all_urls.map {|element|
              h = Hash.new
              h.merge!({
                      "condition":"AND",
                      "rules":[
                          {
                              "value":[
                                  "^https{0,1}:\\/\\/#{element}\\/{0,1}$"
                              ]
                          },
                      ]
                  })
            }
        }
    }
}

结果是:

{: type => "condition",: Params => {: rules => {: condition => "AND",: rules => [{: condition => "AND",
        : rules => [{: value => ["^https{0,1}:\\/\\/example.com/homepage\\/{0,1}$"]
        }]
      }, {: condition => "AND",
        : rules => [{: value => ["^https{0,1}:\\/\\/example.com/another\\/{0,1}$"]
        }]
      }, {: condition => "AND",
        : rules => [{: value => ["^https{0,1}:\\/\\/example.com\\/{0,1}$"]
        }]
      }]
    }
  }
}

each 相同:

all_urls = ["example.com/homepage", "example.com/another", "example.com"]
hash = {
    "type":"condition",
    "Params":{
        "rules":{
            "condition":"AND",
            "rules":
            begin
                  res_lis = []
                  all_urls.each do |element|
                    h = Hash.new
                    h.merge!({
                        "condition":"AND",
                        "rules":[
                            {
                                "value":[
                                    "^https{0,1}:\\/\\/#{element}\\/{0,1}$"
                                ]
                            },
                        ]
                    })
                    res_lis.push(h)
                  end
                  res_lis
            end
          }
      }
}

相同的结果:

{: type => "condition",: Params => {: rules => {: condition => "AND",: rules => [{: condition => "AND",
        : rules => [{: value => ["^https{0,1}:\\/\\/example.com/homepage\\/{0,1}$"]
        }]
      }, {: condition => "AND",
        : rules => [{: value => ["^https{0,1}:\\/\\/example.com/another\\/{0,1}$"]
        }]
      }, {: condition => "AND",
        : rules => [{: value => ["^https{0,1}:\\/\\/example.com\\/{0,1}$"]
        }]
      }]
    }
  }
}

答案 1 :(得分:1)

#map一个镜头:

a = ["example.com/homepage", "example.com/another", "example.com"]
hash = {
    "type":"condition",
    "Params":{
      "rules": {
        "condition":"AND",
        "rules":
          a.map do |url|
            {
              "condition":"AND",
              "rules": {
                "value":[
                  "^https{0,1}:\\/\\/#{url}\\/{0,1}$"
                ]
              },
            }
          end
        }
      }
    }

输出:

 1|    {:type=>"condition",
 2|     :Params=>
 3|      {:rules=>
 4|        {:condition=>"AND",
 5|         :rules=>[
 6|            {:condition=>"AND",
 7|             :rules=>{
 8|               :value=>["^https{0,1}:\\/\\/example.com/homepage\\/{0,1}$"]
 9|              }
10|            },
11|            {:condition=>"AND",
12|             :rules=>{
13|               :value=>["^https{0,1}:\\/\\/example.com/another\\/{0,1}$"]
14|             }
15|           },
16|           {:condition=>"AND",
17|             :rules=>{
18|               :value=>["^https{0,1}:\\/\\/example.com\\/{0,1}$"]
19|             }
20|           }
21|         ]
22|       }
23|    }

与您期望的输出相比,如果有更少的数组,它基本上是相同的...所以我不确定断开连接的位置:

    1|        {"type":"URLS",
    2|         "Params":{
    3|           "rules":{
    4|           "condition":"AND",
    5|           "rules":[
    6|             {"condition":"AND",
    7|              "rules":[
    8|                {
    9|                  "value":["^https{0,1}:\\/\\/example.com/homepage\\/{0,1}$"]
   10|                 }
   11|               ]
   12|              },
   13|              {"condition":"AND",
   14|               "rules":[
   15|                 {
   16|                   "value":["^https{0,1}:\\/\\/example.com/another\\/{0,1}$"]
   17|                 }
   18|               ]
   19|              },
   20|              {"condition":"AND",
   21|               "rules":[
   22|                 {
   23|                   "value":["^https{0,1}:\\/\\/example.com\\/{0,1}$"]
   24|                 }
   25|               ]
   26|             }
   27|           ]
   28|         }
   29|       }
   30|     }