代码之家  ›  专栏  ›  技术社区  ›  Dr. Frankenstein

这个红宝石土豆泥怎么了?

  •  0
  • Dr. Frankenstein  · 技术社区  · 16 年前

    我对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 回复  |  直到 16 年前
        1
  •  6
  •   sepp2k    16 年前

    在Ruby中,大括号用于描述由成对的键和值组成的哈希图。方括号用于描述数组。您的子属性不包含键值对,因此您必须生成一个数组而不是哈希。

    因此,而不是

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

    做:

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

    另一次同样发生 :children .

    我也不确定 primary[0] = 是应该实现的,但它几乎肯定不会做你想做的。它所做的是将 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 什么都不做。

        2
  •  1
  •   Sindri Guðmundsson    16 年前

    在第一个哈希表中,您有

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

    您的:children散列应该是一个数组,由方括号而不是大括号组成:)

        3
  •  1
  •   Jeriko    16 年前

    我认为您可能会混淆数组和哈希。我认为有两点你可能想用数组 [] 而不是散列 {} . 固定代码如下:

    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 }
                ]
              }]
            }
         ]
    
    推荐文章