gpt4 book ai didi

ruby - 将哈希附加到数组

转载 作者:数据小太阳 更新时间:2023-10-29 08:02:30 25 4
gpt4 key购买 nike

这个练习我有点问题,你能帮我吗?我尝试编辑脚本,但每次创建新实例时,最后一个元素都会覆盖其他元素(即 )

该类必须为后续 block 的组合提供功能:即它必须能够从前一个 block 继承尚未为当前 block 显式定义的指令。

blocks = [] 

blocks << Block.new({ X:0, Y:100, Z:20}) # p blocks:({ X:0, Y:100, Z:20})

blocks << Block.new({X:100}, blocks[-1]) #p blocks:({ X:100, Y:100, Z:20},{ X:100, Y:100, Z:20})

blocks << Block.new({Y:0}, blocks[-1]) #p blocks:({ X:100, Y:0, Z:20},{ X:100, Y:0, Z:20},{ X:100, Y:0, Z:20})

我的问题代码是:

#!/usr/bin/env ruby

class Block


$instance_2 = true
$instance_3 = true


attr_reader :out


def initialize(out, previous = nil )

@out = update(out, previous)
end # end initialize


def update(actual, previous)
puts "IN UPDATE: box is: #{previous}"
box = previous
if box != nil
puts "IF: changing is: #{actual}"
actual.each do |key, value|
box[key] = value
end
box
else
puts "ELSE"
actual
end # end if
end # end update

def inspect
@out
end



end # end Block

blocks = []
puts "\n#----------------------1 INSTANCE----------------------#"
blocks << Block.new({G: "00", X: 100, Y:100, Z: 100})
puts "\n#----------------------blocks element----------------------#"
p blocks
puts "last block is: #{blocks[-1].out}"

puts "\n#----------------------2 INSTANCE----------------------#" if $instance_2
blocks << Block.new({G: "01"}, blocks[-1].out) if $instance_2
puts "\n#----------------------blocks element----------------------#"if $instance_2
p blocks if $instance_2
puts "last block is: #{blocks[-1].out}"


puts "\n#----------------------3 INSTANCE----------------------#" if $instance_3
blocks << Block.new({G: "02"}, blocks[-1].out) if $instance_3
puts "\n#----------------------blocks element----------------------#" if $instance_3
p blocks if $instance_3
puts "last block is: #{blocks[-1].out}"

puts "\n#----------------------4 INSTANCE----------------------#" if $instance_3
blocks << Block.new({G: "03"}, blocks[-1].out) if $instance_3
puts "\n#----------------------blocks element----------------------#" if $instance_3
p blocks if $instance_3
puts "last block is: #{blocks[-1].out}"

最佳答案

这个问题不是很清楚,但是如果我理解正确的话,可以提供一个可以接受也可以不接受前一个 block 的类。你觉得这样的事情怎么样?

#!/usr/bin/env ruby

class Block < Hash

def initialize(h, b=nil)
[:x, :y, :z].each do |s|
# We start by trying to assign the coordinate that is in the
# input hash
if h[s]
self[s] = h[s]
else
# If the coordinate is not in h, we check for it in b, but we have to
# remember that if the block that we are providing in b does not have
# the method :[] or the coordinate is nil we may raise an exception
begin
self[s] = b[s]
raise if not self[s]
rescue
raise(Exception, "Cannot initialize block.")
end
end
end
end

def inspect
"block:(x: #{self[:x]}, y: #{self[:y]}, z: #{self[:z]}"
end
end

# Let's try it!
blocks = []
blocks << Block.new({ x:0, y:100, z:20})
puts blocks
puts
blocks << Block.new({x:100}, blocks[-1])
puts blocks
puts
blocks << Block.new({y:0}, blocks[-1])
puts blocks
puts

关于您的代码

我们只考虑更新方法:

def update(actual, previous)
puts "IN UPDATE: box is: #{previous}"
box = previous # ! ! ! ! ! ! ! ! WHOOPS!
if box != nil
puts "IF: changing is: #{actual}"
actual.each do |key, value|
box[key] = value
end
box
else
puts "ELSE"
actual
end # end if
end # end update

“哎呀”行是造成您问题的原因。用那条线您正在将对 previous 的引用(您的对象变量实际上包含一个引用)提供给 box 变量。当您对 box 执行某些操作时,您实际上正在做的是修改两个变量指向的内容。

您可以立即测试我所说的内容。尝试以这种方式修改该行:

box = previous.clone if previous

(nil 没有 #clone 方法,if 背后的原理)。如果你再次运行它,你会得到一个没有被修改的 block 列表。这不是一段高效的代码,您真的应该重新考虑 update 方法代码。

关于ruby - 将哈希附加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48722699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com