Ruby 这个红宝石杂烩怎么了?
我是ruby的新手,经常会遇到以下错误:Ruby 这个红宝石杂烩怎么了?,ruby,hash,Ruby,Hash,我是ruby的新手,经常会遇到以下错误: 在gem\u original\u require':/helpers/navigation.rb:28:散列的奇数列表(SyntaxError) 感谢您的帮助 module Sinatra::Navigation def navigation @navigation nav = { primary[0] = {
在gem\u original\u require':/helpers/navigation.rb:28:散列的奇数列表(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 }
}
}
}
}
}
在ruby中,大括号用于描述由成对的键和值组成的hashmap。方括号用于描述数组。您的children属性不包含键值对,因此必须将其转换为数组而不是散列 所以不是
:children => {
{ :title => "cheese", :active => false },
{ :title => "ham", :active => false }
}
做:
对于其他出现的:children
,情况也是如此
我也不确定primary[0]=
应该实现什么,但几乎可以肯定,它并没有达到您想要的效果。它所做的是将primary
的第一个元素设置为assign(这意味着在该赋值之前必须存在一个名为primary的数组),然后返回该元素
如果您想构造哈希,以便可以像nav[:primary][0][:children][0]
那样访问它,您必须这样做:
nav = {
:primary => [
{:title => "cheddar",
:active => false,
:children => [
{ :title => "cheese", :active => false },
{ :title => "ham", :active => false }
]
},
{
:title => "gorgonzola",
#...
}]
}
还请注意,在分配给
nav
之前的行@navigation
完全没有任何作用。在第一个散列中,您有
:children => {
{ :title => "cheese", :active => false },
{ :title => "ham", :active => false }
}
你的:children散列应该是一个数组,用方括号而不是大括号构造:)我想你可能把数组和散列混淆了。(我认为)有两点可能需要使用数组
[]
而不是散列{}
。固定代码如下:
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 }
]
}]
}
]
所有答案看起来都是正确的,所以感谢大家,这是最快的,非常简洁,现在扩展了,再次感谢这个答案,你真的超越了职责的召唤,你的建议帮了我很多忙,我已经以这种方式重组了它,而且它的方式更加简单。这个缩进太可怕了!
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 }
]
}]
}
]