Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.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内部访问数组中的数据_Ruby On Rails_Ruby_Arrays_Each - Fatal编程技术网

Ruby on rails 在Ruby内部访问数组中的数据

Ruby on rails 在Ruby内部访问数组中的数据,ruby-on-rails,ruby,arrays,each,Ruby On Rails,Ruby,Arrays,Each,我在Ruby中访问“each”语句中的数据时遇到问题。我从SQL查询中获取数据 mysql> select * from mantis_bug_relationship_table WHERE relationship_type = 2 AND destination_bug_id = 753; +-----+---------------+--------------------+-------------------+ | id | source_bug_id | destinati

我在Ruby中访问“each”语句中的数据时遇到问题。我从SQL查询中获取数据

mysql> select * from mantis_bug_relationship_table WHERE relationship_type = 2 AND destination_bug_id = 753;
+-----+---------------+--------------------+-------------------+
| id  | source_bug_id | destination_bug_id | relationship_type |
+-----+---------------+--------------------+-------------------+
| 103 |           765 |                753 |                 2 |
+-----+---------------+--------------------+-------------------+
然后我将每个结果添加到一个数组中,就像这样,关系类型为2

parent_map = {}
current = 1

# for each loop is here that populates parent_map

parent_map[current] = { issues_map[relation.destination_bug_id] => issues_map[relation.source_bug_id] }
current += 1

# for each loop is here that populates parent_map
然后,我尝试从父映射读取数据,如下所示:

parent_map.each do |child, parent|
    pp parent_map   
    print "child: #{child}\n"
    print "parent: #{parent}\n"
    print "---------------------------------------\n"
    STDOUT.flush
  end
{1=>{753=>765}}
child: 1
parent: 753765
这一结果如下:

parent_map.each do |child, parent|
    pp parent_map   
    print "child: #{child}\n"
    print "parent: #{parent}\n"
    print "---------------------------------------\n"
    STDOUT.flush
  end
{1=>{753=>765}}
child: 1
parent: 753765
输出应为:

child: 753
parent: 765

我应该如何访问子级和父级?

在您的示例中,您实际上是在处理哈希,而不是数组

array = []
hash = {}
在您的
父映射中。每个
循环您都在获取键和值。您的密钥由初始填充循环中的
current
变量填充,而您的值也是一个包含要访问的父级和子级的哈希

假设您需要作为值的散列,则需要一个子循环,ala:

parent_map.each do |key, val| # This val is your hash: {753=>765}
  val.each do |child, parent|
    puts "child: #{child}" # 753
    puts "parent: #{parent}" # 765
  end
end

实际上,您在示例中处理的是哈希,而不是数组

array = []
hash = {}
在您的
父映射中。每个
循环您都在获取键和值。您的密钥由初始填充循环中的
current
变量填充,而您的值也是一个包含要访问的父级和子级的哈希

假设您需要作为值的散列,则需要一个子循环,ala:

parent_map.each do |key, val| # This val is your hash: {753=>765}
  val.each do |child, parent|
    puts "child: #{child}" # 753
    puts "parent: #{parent}" # 765
  end
end
这应该行得通


这应该是可行的。

您不需要像其他答案那样使用嵌套循环。取第二个参数并对其进行分解

parent_map.each do |_, (child, parent)|
  pp parent_map
  puts "child: #{child}"
  puts "parent: #{parent}"
  puts "---------------------------------------"
  STDOUT.flush
end

您不需要像在其他答案中那样使用嵌套循环。取第二个参数并对其进行分解

parent_map.each do |_, (child, parent)|
  pp parent_map
  puts "child: #{child}"
  puts "parent: #{parent}"
  puts "---------------------------------------"
  STDOUT.flush
end

您已经在访问子级和父级。你到底想用它做什么?你能提供你期望的输入和输出吗?我已经用输出应该是什么编辑了我的问题。当向父映射添加数据时,我实际上是在添加{'753'>='765'}您已经在访问子映射和父映射。你到底想用它做什么?你能提供你期望的输入和输出吗?我已经用输出应该是什么编辑了我的问题。在向父映射添加数据时,我实际上是在添加{'753'>='765'}谢谢!非常感谢。太棒了,我不知道你能做到。下划线是否跳过将键分配给变量?是。下划线是一种约定,用于接收不使用的变量。@sawa不知何故,它对我不起作用,除非出于某种原因,我不知道你可以这样做。下划线是否跳过将键分配给变量?是。下划线是一种约定,用于接收不使用的变量。@sawa除非有任何原因,否则它对我不起作用