私はファイルから入力を読み込むコードを書いています。2次元配列が上書きされるべきでないときに上書き
私のコードは入力ファイルの内容を解析し、そのデータを2D配列として格納します。
入力されている(私はここで動作するように書式設定を取得することはできません、正しい書式については、以下の入力ファイルを参照してください):
ABC DEF
G
2次元配列を解析すると、次のようになります。 [['A
私が問題にしていることは、何らかの形で以前の要素が上書きされているということです2D配列内で、次のエントリ、例えば
[[ 'G']、[ 'G']、[ 'G']]
2D配列がすべきと書き込みとして、私は、それを見てきたが、これが起こっているかを確認することはできません新しいエントリごとに1回だけ発生し、新しいデータを2D配列に追加して以前のエントリを上書きしないでください。
私はちょっと立ち往生していますが、なぜこのようなことが起こっているかについてのアイデアはありますか?
感謝!:)
コード
class Reader
def initialize
@command_array = Array.new { Array.new } # 2D array
end
def run(file)
return puts "please provide correct file" if file.nil? || !File.exists?(file)
command_line = Array.new #Temp array
p "----------------------------------------------------------------"
File.open(file).each do |line|
p "looking at a line of commands..."
line.split(' ').each do |command|
p "storing the command #{command} in temp array"
command_line.push(command)
p command_line
end
p "Storing the temp array as an element in the 2d array..."
@command_array.push(command_line)
p @command_array
p "Clearing the temp array..."
p "----------------------------------------------------------------"
command_line.clear
end
end
end
#
入力ファイル
A B C
D E F
G
#
出力
"looking at a line of commands..."
"storing the command A in temp array"
["A"]
"storing the command B in temp array"
["A", "B"]
"storing the command C in temp array"
["A", "B", "C"]
"Storing the temp array as an element in the 2d array..."
[["A", "B", "C"]]
"Clearing the temp array..."
"----------------------------------------------------------------"
"looking at a line of commands..."
"storing the command D in temp array"
["D"]
"storing the command E in temp array"
["D", "E"]
"storing the command F in temp array"
["D", "E", "F"]
"Storing the temp array as an element in the 2d array..."
[["D", "E", "F"], ["D", "E", "F"]]
"Clearing the temp array..."
"----------------------------------------------------------------"
"looking at a line of commands..."
"storing the command G in temp array"
["G"]
"Storing the temp array as an element in the 2d array..."
[["G"], ["G"], ["G"]]
"Clearing the temp array..."
#
'px'はデバッグに使用することを意図しています。これは' puts x.inspect'と同じです。引用符を避ける 'puts'でプロンプトを表示する必要があります。 – tadman
ちょっと@tadman、あなたの入力に感謝します。私はちょうどスクリーンに配列の内容を投げるための素早い方法として 'p'を使用していました。これを見ていただきありがとうございます:) – TheLemonSong