tags:

views:

62

answers:

3

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 }
                            }
                           }
                          }
                        }
                }
+5  A: 

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.

sepp2k
And the main `nav` variable?
MvanGeest
All answers look right so thanks to all, this was fastest and was really concise, now expanded, appreciated
Dr. Frankenstein
Thanks again for this answer, you've really looked beyond the call of duty here, your advice has helped me no end, I've restructured it this way and its way more straightforward.
Dr. Frankenstein
+1  A: 

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 :)

Sindri Guðmundsson
Btw, you do this more than this one time in your code :)
Sindri Guðmundsson
+1  A: 

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