这个红宝石哈希有什么问题吗?

时间:2010-06-18 13:08:47

标签: ruby hash

我对ruby很新,我不断收到以下错误:

in gem_original_require': ./helpers/navigation.rb:28: odd number list for Hash (SyntaxError)

任何帮助表示赞赏...

   module Sinatra::Navigation

        def navigation

            @navigation

                nav = {

                        primary[0] = {
                         :title => "cheddar",
                         :active => false,
                         :children => {
                           { :title => "cheese", :active => false },
                           { :title => "ham", :active => false }
                          }
                        },

                        primary[1] = {
                         :title => "gorgonzola",
                         :active => false,
                         :children => {
                           { :title => "What is the cheese?", :active => false },
                           { :title => "What cheese", :active => false },
                           { :title => "What does the cheese tell us?", :active => false, :children => {
                              { :title => "Cheessus", :active => false },
                              { :title => "The impact of different cheeses / characteristics for cheese in relation to CHSE outcomes", :active => false }
                            }
                           }
                          }
                        }
                }

3 个答案:

答案 0 :(得分:6)

在ruby花括号中用于描述由键和值对组成的散列映射。方括号用于描述数组。您的children属性不包含键值对,因此您必须使用数组而不是哈希值。

所以而不是

:children => {
  { :title => "cheese", :active => false },
  { :title => "ham", :active => false }
}

做的:

:children => [
  { :title => "cheese", :active => false },
  { :title => "ham", :active => false }
]

对于:children的另一次出现也一样。

我也不确定primary[0] =应该达到什么目标,但它几乎肯定不会达到您想要的效果。它所做的是将assign设置为primary的第一个元素(这意味着在该赋值之前必须存在一个名为primary的数组),然后返回该元素。

如果你想构建你的哈希,以便像nav[:primary][0][:children][0]那样访问它,你必须这样做:

nav = {
  :primary => [
    {:title => "cheddar",
     :active => false,
     :children => [
                    { :title => "cheese", :active => false },
                    { :title => "ham", :active => false }
                  ]
    },
    {
       :title => "gorgonzola",
       #...
    }]
}

另请注意,分配给@navigation之前的行nav根本不执行任何操作。

答案 1 :(得分:1)

在第一个哈希中,你有

:children => {
    { :title => "cheese", :active => false },
    { :title => "ham", :active => false }
}

你的:children hash应该是一个数组,用方括号而不是花括号构造:)

答案 2 :(得分:1)

我认为你可能会将数组与哈希相混淆。 (我认为)有两点你可能想要使用数组[]而不是哈希{}。修正代码如下:

nav = [
       { :title => "cheddar",
         :active => false,
         :children => [
            { :title => "cheese", :active => false },
            { :title => "ham", :active => false }
           ]
        },
        { :title => "gorgonzola",
          :active => false,
          :children => [
           { :title => "What is the cheese?", :active => false },
           { :title => "What cheese", :active => false },
           { :title => "What does the cheese tell us?", :active => false, 
             :children => [
               { :title => "Cheessus", :active => false },
               { :title => "The impact of different cheeses / characteristics for cheese in relation to CHSE outcomes", :active => false }
            ]
          }]
        }
     ]