我有一个函数,它将方程作为字符串数组,并且(我希望)用它们的值替换任何已知变量(puts 语句仅用于调试):
def replace_known_variables
@equation_array.map! do |x|
for known_var in @known_variables
puts "@equation_array is initially #@equation_array"
if known_var.full_name == x
x = known_var.value.to_f
puts "x is now #{x}"
else
x = x
puts "x is still #{x}"
end
end
end
puts "@equation_array is now #@equation_array"
end
这可能不是处理这种情况的“Ruby 方式”,因此欢迎任何建议,但我的问题是,当我实际运行它时,看起来 x 已正确设置,但是当一切都说完了之后,我的@equation_array 最终保存known_var(我创建的变量类)使用的类的实例,而不是我期望的字符串。在我看来 x 实际上是作为实例存储回来的。例如:
@equation_array is initially ["amperage", "*", "resistance"]
x is now 12.0
@equation_array is initially ["amperage", "*", "resistance"]
x is still 12.0
@equation_array is initially [[#<Variable:0x000001c28755c490 @abbrev="i", @full_name="amperage", @unit="A", @value="12">, #<Variable:0x000001c28745a060 @abbrev="r", @full_name="resistance", @unit="Ohms", @value="2200">], "*", "resistance"]
x is still *
@equation_array is initially [[#<Variable:0x000001c28755c490 @abbrev="i", @full_name="amperage", @unit="A", @value="12">, #<Variable:0x000001c28745a060 @abbrev="r", @full_name="resistance", @unit="Ohms", @value="2200">], "*", "resistance"]
x is still *
@equation_array is initially [[#<Variable:0x000001c28755c490 @abbrev="i", @full_name="amperage", @unit="A", @value="12">, #<Variable:0x000001c28745a060 @abbrev="r", @full_name="resistance", @unit="Ohms", @value="2200">], [#<Variable:0x000001c28755c490 @abbrev="i", @full_name="amperage", @unit="A", @value="12">, #<Variable:0x000001c28745a060 @abbrev="r", @full_name="resistance", @unit="Ohms", @value="2200">], "resistance"]
x is still resistance
@equation_array is initially [[#<Variable:0x000001c28755c490 @abbrev="i", @full_name="amperage", @unit="A", @value="12">, #<Variable:0x000001c28745a060 @abbrev="r", @full_name="resistance", @unit="Ohms", @value="2200">], [#<Variable:0x000001c28755c490 @abbrev="i", @full_name="amperage", @unit="A", @value="12">, #<Variable:0x000001c28745a060 @abbrev="r", @full_name="resistance", @unit="Ohms", @value="2200">], "resistance"]
x is now 2200.0
@equation_array is now [[#<Variable:0x000001c28755c490 @abbrev="i", @full_name="amperage", @unit="A", @value="12">, #<Variable:0x000001c28745a060 @abbrev="r", @full_name="resistance", @unit="Ohms", @value="2200">], [#<Variable:0x000001c28755c490 @abbrev="i", @full_name="amperage", @unit="A", @value="12">, #<Variable:0x000001c28745a060 @abbrev="r", @full_name="resistance", @unit="Ohms", @value="2200">], [#<Variable:0x000001c28755c490 @abbrev="i", @full_name="amperage", @unit="A", @value="12">, #<Variable:0x000001c28745a060 @abbrev="r", @full_name="resistance", @unit="Ohms", @value="2200">]]
我已经尝试了几件事(在所有赋值中显式指定 to_ 类型,删除值保存中的 to_f ),但没有成功,而且我基本上不确定我做错了什么(除了可能是一个令人费解的问题)总体方法论)。我可以通过创建临时哈希或其他东西来解决它,但我宁愿了解我的预期流程出了什么问题。
“If case” 是 Ruby 中的表达式,返回结果。由于您使用了map!,它会将返回值替换为原始值。所以尝试下面的代码。
def replace_known_variables
@equation_array.map! do |x|
known_var = @known_variables.find { |var| var.full_name == x }
known_var ? known_var.value.to_f : x
end
end