小编Zac*_*emp的帖子

ruby数组键值对?

我试图配对两个键值对,但我不确定如何实现这一点.以下是我的尝试:

struc = Array[(3,4),(5,6)]
for i in 0..1
    puts "#{struc[i,i]}"
end
Run Code Online (Sandbox Code Playgroud)

但我想要的输出如下(前一个代码块不生成):

3 4
5 6
Run Code Online (Sandbox Code Playgroud)

ruby arrays hash

9
推荐指数
2
解决办法
2万
查看次数

Ruby方法返回哈希值为{}

变量valueinitialize方法LocationList填充在这些变化是由relected线014 print语句线015,但return在线路016认为哈希仍然是空的(向右滚动才能看到后返回值=>).

def random_point
  x = rand * 2.0 - 1.0
  y = rand * 2.0 - 1.0
  until x**2 + y**2 < 1.0
    x = rand * 2.0 - 1.0
    y = rand * 2.0 - 1.0
  end
  return [x, y]
end

class LocationList < Hash
  def initialize(node_list)
    value = {}
    node_list.each {|node| value[node] = random_point }
    print value
    return value
  end
end

z = ["moo", "goo", "gai", …
Run Code Online (Sandbox Code Playgroud)

ruby hash return instantiation

-1
推荐指数
1
解决办法
9162
查看次数

标签 统计

hash ×2

ruby ×2

arrays ×1

instantiation ×1

return ×1