Ruby文件处理,读取然后存储为数组



我不明白这段代码是如何工作的,程序可以读取文件,但最终会多次打印同一行。

输出:

Track title is: c/music/1
Track file location is: c/music/1
Track title is: c/music/2 
Track file location is: c/music/2
Track title is: c/music/3
Track file location is: c/music/3

预期输出:

Track title is: Taco
Track file location is: c/music/1
Track title is: Burrito 
Track file location is: c/music/2
Track title is: Nacho
Track file location is: c/music/3

代码:

class Track
attr_accessor :title, :file_location
end
def read_tracks music_file
count = music_file.gets().to_i
tracks = Array.new
$i = 0
while $i < count do
track = read_track(music_file)
tracks << track
$i += 1
end
tracks
end

def read_track aFile
track_title = aFile.gets
track_file_location = aFile.gets
track = Track.new
track.title = track_title
track.file_location = track_file_location
end

def print_tracks tracks
$i = 0
while $i < tracks.length do
print_track(tracks)
$i += 1
end
end

def print_track tracks
puts('Track title is: ' + tracks[$i].to_s)
puts('Track file location is: ' + tracks[$i].to_s)
end

def main
aFile = File.new("input.txt", "r") 
if aFile  
tracks = read_tracks(aFile)
aFile.close
else
puts "Unable to open file to read!"
end
print_tracks(tracks)
end
main

输入文件示例:

5
Taco
c/music/1
Burrito
c/music/2
Nacho
c/music/3

试试这个,将行复制到数组,然后操作它:

lines = File.readlines('tracks.txt') # reads lines into array
lines.reject! { |e| e == "n" } # removes empti lines
total_tracks = lines.shift.chomp.to_i # extract the first line from the array
lines.each_slice(2) { |e| puts e } # lines now contains only the pair track/directory
# adapt at your will

对于each_slice(2),请参见Enumerable#each_slice,它将数组的元素分组。

问题出现在方法print_trackprint_track
这些方法应该如下所示:

def print_tracks tracks
$i = 0
while $i < tracks.length do
print_track(tracks[$i])
$i += 1
end    
end

def print_track track
puts('Track title is: ' + track.title.to_s)
puts('Track file location is: ' + track.file_location.to_s)
end

但如果你想让你的代码变得更好,可以试试这样的方法:

def print_tracks(tracks)
tracks.each do |track|
puts "Track title is: #{track.title}"
puts "Track file location is: #{track.file_location}"
puts
end
end

在这种情况下,整个代码将是:

class Track
attr_accessor :title, :file_location
end
def read_tracks music_file
count = music_file.gets().to_i
tracks = Array.new
i = 0
while i < count do
track = read_track(music_file)
tracks << track
i += 1
end
tracks
end
def read_track aFile
track = Track.new
track.title = aFile.gets
track.file_location = aFile.gets
track
end
def print_tracks(tracks)
tracks.each do |track|
puts "Track title is: #{track.title}"
puts "Track file location is: #{track.file_location}"
puts
end
end
def main
aFile = File.new("input.txt", "r").
if aFile..
tracks = read_tracks(aFile)
aFile.close
else
puts "Unable to open file to read!"
end
print_tracks(tracks)
end
main

我已经使用示例文件input.txt测试了这个代码:

3
Taco
c/music/1
Burrito
c/music/2
Nacho
c/music/3

我有输出:

Track title is: Taco
Track file location is: c/music/1
Track title is: Burrito
Track file location is: c/music/2
Track title is: Nacho
Track file location is: c/music/3

这正是你所期望的!

相关内容

  • 没有找到相关文章

最新更新