Immer wenn Sie Probleme wie diese haben, ist es lehrreich, einen Code zum Ausdrucken der object_id
jedes Objekts von Interesse in jedem der verschiedenen Schritte in der Berechnung hinzuzufügen. Jedes Ruby-Objekt hat eine eindeutige object_id
.
{ "a"=>1 }.object_id
#=> 70225550848400
Versuchen wir es: Die ID kann Object.object_id mit dem Verfahren abgerufen werden. (Ich habe object_id
‚s verkürzt, um ihre letzten drei Ziffern zu erleichtern, um zu sehen, wenn sie sich ändern.)
array1 = [["a", "b", "c"], ["a", "b", "c"]]
puts "1a array1.object_id=#{array1.object_id % 1000}"
puts "1b array1.map(&:object_id)=#{array1.map { |e| e.object_id % 1000 } }"
puts
temp1 = ["x", "y", "z"]
puts "2a temp1.object_id=#{temp1.object_id % 1000}"
array1 << temp1
puts "2b array1=#{array1.inspect}"
puts "2c array1.object_id=#{array1.object_id % 1000}"
puts "2d array1.map(&:object_id)=#{array1.map { |e| e.object_id % 1000 } }"
puts
2.times do
temp1[0] = gets.chomp
temp1[1] = gets.chomp
temp1[2] = gets.chomp
puts "3a temp1=#{temp1.inspect}"
puts "3b temp1.object_id=#{temp1.object_id % 1000}"
array1 << temp1
puts "3c array1=#{array1.inspect}"
puts "3d array1.object_id=#{array1.object_id % 1000}"
puts "3e array1.map(&:object_id)=#{array1.map { |e| e.object_id % 1000 } }"
puts
end
druckt
1a array1.object_id=900
1b array1.map(&:object_id)=[0, 920]
2a temp1.object_id=480
2b array1=[["a", "b", "c"], ["a", "b", "c"], ["x", "y", "z"]]
2c array1.object_id=900
2d array1.map(&:object_id)=[0, 920, 480]
1
2
3
3a temp1=["1", "2", "3"]
3b temp1.object_id=480
3c array1=[["a", "b", "c"], ["a", "b", "c"], ["1", "2", "3"], ["1", "2", "3"]]
3d array1.object_id=900
3e array1.map(&:object_id)=[0, 920, 480, 480]
4
5
6
3a temp1=["4", "5", "6"]
3b temp1.object_id=480
3c array1=[["a", "b", "c"], ["a", "b", "c"], ["4", "5", "6"], ["4", "5", "6"],
["4", "5", "6"]]
3d array1.object_id=900
3e array1.map(&:object_id)=[0, 920, 480, 480, 480]
Sie müssen dies sorgfältig studieren.
Um zu verstehen, was vor sich geht, kann es hilfreich sein, sich ein Array als Container vorzustellen. Sie haben den Inhalt eines Containers geändert, aber nicht den Container selbst. array1
ist eine Liste von Containern.
Um Ihren Code arbeiten zu lassen, müssen Sie nur den Container und den Inhalt ändern. Ein einfacher Weg ist zu ersetzen:
temp1[0] = gets.chomp
temp1[1] = gets.chomp
temp1[2] = gets.chomp
mit
temp1 = [gets.chomp, gets.chomp, gets.chomp]
array1 = [["a", "b", "c"], ["a", "b", "c"]]
puts "1a array1.object_id=#{array1.object_id % 1000}"
puts "1b array1.map(&:object_id)=#{array1.map { |e| e.object_id % 1000 } }"
puts
temp1 = ["x", "y", "z"]
puts "2a temp1.object_id=#{temp1.object_id % 1000}"
array1 << temp1
puts "2b array1=#{array1.inspect}"
puts "2c array1.object_id=#{array1.object_id % 1000}"
puts "2d array1.map(&:object_id)=#{array1.map { |e| e.object_id % 1000 } }"
puts
2.times do
temp1 = [gets.chomp, gets.chomp, gets.chomp]
puts "3a temp1=#{temp1.inspect}"
puts "3b temp1.object_id=#{temp1.object_id % 1000}"
array1 << temp1
puts "3c array1=#{array1.inspect}"
puts "3d array1.object_id=#{array1.object_id % 1000}"
puts "3e array1.map(&:object_id)=#{array1.map { |e| e.object_id % 1000 } }"
puts
end
druckt
1a array1.object_id=100
1b array1.map(&:object_id)=[220, 120]
2a temp1.object_id=660
2b array1=[["a", "b", "c"], ["a", "b", "c"], ["x", "y", "z"]]
2c array1.object_id=100
2d array1.map(&:object_id)=[220, 120, 660]
1
2
3
3a temp1=["1", "2", "3"]
3b temp1.object_id=800
3c array1=[["a", "b", "c"], ["a", "b", "c"], ["x", "y", "z"], ["1", "2", "3"]]
3d array1.object_id=100
3e array1.map(&:object_id)=[220, 120, 660, 800]
4
5
6
3a temp1=["4", "5", "6"]
3b temp1.object_id=580
3c array1=[["a", "b", "c"], ["a", "b", "c"], ["x", "y", "z"], ["1", "2", "3"],
["4", "5", "6"]]
3d array1.object_id=100
3e array1.map(&:object_id)=[220, 120, 660, 800, 580]
Sie anhängt und die gleiche Anordnung zu ändern. –
Warum ist es nicht sequentiell, auch wenn ich an das gleiche Array angehängt bin? (also die erste Schleife, die ich 1,2,3 an Array1 anfüge und dann 4,5,6 anfüge) (ich bin ein Anfänger) – Breezecom
Nein, Sie fügen * hinzu und mutieren * das gleiche Array. Sie fügen es einmal an, wenn es 123 ist. Dann ändern Sie es in 456, was bedeutet, dass die bereits angehängte Datei in 456 geändert wird, und Sie dann erneut anhängen. –