-2

我想填充一个具有整数和散列的数组,例如:

my_a=[Integer,{}]

然后例如我试图拥有:

my_a[5,{:direction=>'up'}]
my_a[5,{:speed=>'fast'}]
my_a[3,{:direction=>'up'}]
my_a[3,{:speed=>'slow'}]

但我明白了

ArgumentError: wrong number of arguments(2 for 1)

如何使用 :direction => 'up' 将 my_a 设置为 5 的条目?

也许整个事情应该是一个哈希?

试图找到某种方式来存储它:

[0,{:top => ' ―― '},{:top_mid => '|__|'},{:bot_mid => '|  |'},{:bot => '――'}]
[1,{:top => ' ―― '},{:top_mid => '|__|'},{:bot_mid => '|  |'},{:bot => '――'}]
[2,{:top => ' ―― '},{:top_mid => '|__|'},{:bot_mid => '|  |'},{:bot => '――'}]
4

4 回答 4

4

我认为您正在寻找哈希数组。或者,如果数字索引不连续,则使用哈希散列。

要创建这样的数组,您可以使用数组文字 ( [...]) 和哈希文字 ( {...}) 内部:

my_arr = [
  {:top => 'top0', :bot => 'bot0'},
  {:top => 'top1', :bot => 'bot1'}
]

(空格可选)。然后my_arr[0]将引用第一个哈希(withtop0bot0inside),my_arr[1]并将引用第二个哈希。my_arr[0][:bot]将引用:bot第一个哈希中的值,bot0.

请注意,因为ismy_arr[2][:bot]会引发异常。如果您通过索引访问,请确保包括任何必要的检查。my_arr[2]nil

看:

于 2013-06-15T15:09:21.000 回答
3

你写了:

试图找到某种方式来存储它:

[0,{:top => ' ―― '},{:top_mid => '|__|'},{:bot_mid => '|  |'},{:bot => '――'}]
[1,{:top => ' ―― '},{:top_mid => '|__|'},{:bot_mid => '|  |'},{:bot => '――'}]
[2,{:top => ' ―― '},{:top_mid => '|__|'},{:bot_mid => '|  |'},{:bot => '――'}]

我不认为那是你真正想要的。你展示的是一组离散的数组,每个数组都有一个整数和一堆单元素哈希。我你真的想要这个:

a = [
  {:top => ' ―― ', :top_mid => '|__|', :bot_mid => '|  |', :bot => '――'},
  {:top => ' ―― ', :top_mid => '|__|', :bot_mid => '|  |', :bot => '――'},
  {:top => ' ―― ', :top_mid => '|__|', :bot_mid => '|  |', :bot => '――'}
]

使用上述结构数据,您可以按索引和名称请求项目,如下所示:

puts a[1][:top] #=> ' ―― '

您可以像我上面展示的那样直接创建它,也可以像这样添加它:

a = [] # Just an array; the contents are arbitrary

# Add an entire row at once…
a[0] = {:top => ' ―― ', :top_mid => '|__|', :bot_mid => '|  |', :bot => '――'}

# …or add to it piecemeal
a[1] = {} # An empty hash, waiting to be filled
a[1][:top] = ' ―― '
a[1][:bot] = ' ―― '
# et cetera

请注意,如果您不直接知道每个条目的索引,而只是想在末尾添加行,您可以这样做:

a << {} # push this hash onto the end of the array
a.last[:top] = ' ―― '
# and so on
于 2013-06-15T15:29:05.373 回答
0

似乎您想将特定结构存储在数组或哈希中。如果您不想为此创建一个类,可以使用OpenStruct

require 'ostruct'
item = OpenStruct.new(top: ' ―― ', top_mid: '|__|', bot_mid: '|  |', bot: '――')
#=> #<OpenStruct top=" ―― ", top_mid="|__|", bot_mid="|  |", bot="――">, 1=>#<OpenStruct top=" ―― ", top_mid="|__|", bot_mid="|  |", bot="――">

您可以像这样获取和设置属性:

item.top                    # get "top" value
# => ' ―― '

item.top = "other value"    # set "top" value

多个项目可以存储在一个数组中:

array = []   # shortcut for Array.new

10.times { |index|
  array << OpenStruct.new(top: ' ―― ', top_mid: '|__|', bot_mid: '|  |', bot: '――')
}

array        #=> [<OpenStruct ...>, 2=>#<OpenStruct ...>, ...]
array[0]     #=> <OpenStruct ...>
array[0].top #=> ' ―― '

或者在带有数字键的哈希中:

hash = {}    # shortcut for Hash.new

1.upto(10) { |index|
  hash[index] = OpenStruct.new(top: ' ―― ', top_mid: '|__|', bot_mid: '|  |', bot: '――')
}

hash         #=> {1=>#<OpenStruct ...>, 2=>#<OpenStruct ...>, ...}
hash[1]      #=> <OpenStruct ...>
hash[1].top  #=> ' ―― '
于 2013-06-15T15:09:37.827 回答
0

您正在尝试使用散列数组的值存储散列。尝试这个:

my_a = {}
my_a[0] = [{:top => ' ―― '},{:top_mid => '|__|'},{:bot_mid => '|  |'},{:bot => '――'}]
my_a[1] = [{:top => ' ―― '},{:top_mid => '|__|'},{:bot_mid => '|  |'},{:bot => '――'}]
my_a[2] = [{:top => ' ―― '},{:top_mid => '|__|'},{:bot_mid => '|  |'},{:bot => '――'}]

除非你想要一个哈希值......

my_a = {}
my_a[0] = {:top => ' ―― ', :top_mid => '|__|', :bot_mid => '|  |', :bot => '――'}
my_a[1] = {:top => ' ―― ', :top_mid => '|__|', :bot_mid => '|  |', :bot => '――'}
my_a[2] = {:top => ' ―― ', :top_mid => '|__|', :bot_mid => '|  |', :bot => '――'}

还要记住,Ruby 是动态类型的,您不能声明存储在数组或散列中的类型。

于 2013-06-15T15:10:04.773 回答