ruby - 如何使用整数和散列设置混合数组

时间:2013-06-15 14:28:34

标签: ruby hash

我想填充一个包含整数和散列的数组,如:

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)

如何设置my_a以获得5的条目:direction => '向上'?

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

试图找到存储它的东西:

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

4 个答案:

答案 0 :(得分:4)

我认为你正在寻找一系列哈希。或者,如果数字索引不是连续的,则是散列哈希值。

要创建这样的数组,您可以在内部使用带有散列文字([...])的数组文字({...}):

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

(空格可选)。然后my_arr[0]将引用第一个哈希值(内部带有top0bot0),my_arr[1]将引用第二个哈希值。 my_arr[0][:bot]将引用第一个哈希值:bot中的bot0值。

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

请参阅:

答案 1 :(得分: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

答案 2 :(得分: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  #=> ' ―― '

答案 3 :(得分: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是动态类型的,而不能声明存储在数组或散列中的类型。