Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用ruby on rails的多维数组_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 使用ruby on rails的多维数组

Ruby on rails 使用ruby on rails的多维数组,ruby-on-rails,ruby,Ruby On Rails,Ruby,我必须对目的地的哈希进行重新排序,因此我想在如下数组中创建一个数组: @orderedDestinations = Array.new @destinations.each do |destination| if (destination.position != nil) @orderedDestinations[destination.position][destination.id] = destination end end 我得到了这个错误: You have a n

我必须对目的地的哈希进行重新排序,因此我想在如下数组中创建一个数组:

@orderedDestinations = Array.new 
@destinations.each do |destination|
  if (destination.position != nil)
    @orderedDestinations[destination.position][destination.id] = destination
  end
end
我得到了这个错误:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]=
我做错了什么?

是的,
   @orderedDestinations[destination.position] is nil so: 

   @orderedDestinations[destination.position][destination.id] really is:

   -> nil[destination.id]
非常感谢。解决方案是添加以下行:

@orderedDestinations[destination.position] ||= {}
因此,完整的代码是:

@orderedDestinations = Array.new
@destinations.each do |destination|
  if (destination.position != nil)
    @orderedDestinations[destination.position] ||= {}
    @orderedDestinations[destination.position][destination.id] = destination
  end
end
谢谢。

是的, 非常感谢。解决方案是添加以下行:

@orderedDestinations[destination.position] ||= {}
因此,完整的代码是:

@orderedDestinations = Array.new
@destinations.each do |destination|
  if (destination.position != nil)
    @orderedDestinations[destination.position] ||= {}
    @orderedDestinations[destination.position][destination.id] = destination
  end
end
谢谢。

在Ruby中,除非显式初始化为某物,否则大多数东西都是零。例如,如果一个新数组的所有元素都不存在或以前没有分配给它们,则默认情况下都是这样。像这样:

test = [ 1, 2 ]
# => [1,2]
test[1]
# => 2
test[2]
# => nil
您可能要做的是根据需要初始化数组的第二级。您可以采用如下模式:

@orderedDestinations = [ ] # Empty array
@destinations.each do |destination|
  if (destination.position)
    # If this element of the array has not been initialized,
    # populate it with a new empty array.
    destination_set = @orderedDestinations[destination.position] ||= [ ]

    # Put something in this second-level array spot
    destination_set[destination.id] = destination
  end
end
第二级条目的数组[]或哈希{}的选择取决于存储在其中的数据类型。哈希表可以轻松地处理任意标识符,其中数组与通常从零开始或接近零的序列号配合使用效果最好。如果初始化数组的元素X,该数组将自动变为大小X+1。

在Ruby中,除非显式初始化为某个值,否则大多数值都为零。例如,如果一个新数组的所有元素都不存在或以前没有分配给它们,则默认情况下都是这样。像这样:

test = [ 1, 2 ]
# => [1,2]
test[1]
# => 2
test[2]
# => nil
您可能要做的是根据需要初始化数组的第二级。您可以采用如下模式:

@orderedDestinations = [ ] # Empty array
@destinations.each do |destination|
  if (destination.position)
    # If this element of the array has not been initialized,
    # populate it with a new empty array.
    destination_set = @orderedDestinations[destination.position] ||= [ ]

    # Put something in this second-level array spot
    destination_set[destination.id] = destination
  end
end
第二级条目的数组[]或哈希{}的选择取决于存储在其中的数据类型。哈希表可以轻松地处理任意标识符,其中数组与通常从零开始或接近零的序列号配合使用效果最好。如果初始化数组的元素X,则该数组将自动变为大小X+1。

如果要按Destinationposition对@destinations进行排序,只需执行以下操作:

@orderedDestinations = @destinations.sort_by(&:position)
完成交易。

如果要按目的地位置对@Destinationposition进行排序,只需执行以下操作:

@orderedDestinations = @destinations.sort_by(&:position)


完成交易。

然后您正在创建散列。数组和散列不一样。考虑到任意id值被用作此处的键,我认为这是合理的做法。@tadman我指出他正在创建散列,而不是数组,因为不清楚他是否知道存在差异。然后你正在创建散列。数组和哈希不一样。考虑到任意id值被用作这里的键,我认为这样做是合理的。@tadman我指出他正在创建哈希而不是数组,因为他不清楚是否知道有区别。是的,但这实际上更多的是一个注释而不是一个答案。不是,这就是答案。他问他做错了什么,我告诉了他。简洁明了,切中要害……不必把事情说得太长。否决票真的没有必要。通常当有人问他们做错了什么时,告诉他们如何做才是正确的。我同意没有必要投反对票,但不是我是的,但这实际上更多的是一个评论而不是一个答案。不是真的,这就是答案。他问他做错了什么,我告诉了他。简洁明了,切中要害……不必把事情说得太长。否决票真的没有必要。通常当有人问他们做错了什么时,告诉他们如何做才是正确的。我同意没有必要投反对票,但不是我你真的不能花太多的时间去学习枚举可以为你做什么。你真的不能花太多的时间去学习枚举可以为你做什么。离题,但你真的应该使用nil吗?谓词,而不是将对象与nil进行比较;或者甚至不必为任何nil比较而烦恼——如果destination.positionOff-topic,那么您真的应该使用nil吗?谓词,而不是将对象与nil进行比较;或者甚至不考虑任何零比较-如果destination.position