Ruby一个衬垫来代替一个粗短的代码

时间:2014-01-27 19:02:30

标签: ruby-on-rails ruby ruby-on-rails-3

在这里摆脱这个厚重代码的任何一个很棒的衬垫?

hash = {}
hash["permission_type"]  = "some permission type"
hash["vaue"] = "some value"
permission_collection = [] << hash

2 个答案:

答案 0 :(得分:8)

permission_collection = [ {"permission_type" => "some permission type", "vaue" => "some value"} ]

答案 1 :(得分:0)

因为你要求一个单行:

permission_collection = [Hash["permission_type","some_permission_value","value","some_value"]]

IMO,最好的选择是使用Utsav的答案,但是要将哈希值分散到多行,以使其更具可读性。

类似的东西:

permission_collection = [
                          {
                            "permission_type" => "some_permission_value",
                            "value"           => "some_value"
                           }
                        ]

不止一行,但更具可读性。