开发者

whats wrong with this ruby hash?

I'm pretty new to ruby, I keep getting the following error:

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

Any help appreciated...

   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 }
                            }
                           }
                          }
                        }
                }


In ruby curly braces are used to describe a hashmap consisting of pairs of keys and values. Square brackets are used to describe arrays. Your children attribute does not contain key-value-pairs, so you have to make into an array instead of a hash.

So instead of

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

do:

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

And the same for the other occurrence of :children.

I'm also not sure what primary[0] = is supposed to achieve, but it almost certainly doesn't do what you want it to. What it does do is assign is set the first element of primary (which means that an array called primary has to exist before that assignment) and then return that element.

If you want to structure your hash so that it can be accessed like nav[:primary][0][:children][0], you have to do it like this:

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

Also note that the line @navigation right before you assign to nav does nothing at all.


In the first hash, you have

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

Your :children hash should be an array, constructed with square brackets instead of curly braces :)


I think you might be confusing arrays with hashes. There are (i think) two points in which you probably want to use an array [] instead of a hash {}. Fixed code as follows:

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 }
            ]
          }]
        }
     ]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜