代码如下:
Song = Struct.new(:title, :name, :length)
File.open("songdata") do |song_file|
songs = []
song_file.each do |line|
file, length, name, title = line.chomp.split(/\s*\|\s*/)
songs << Song.new(title, name, length)
end
puts songs[1]
end
预期结果:
#<struct Song title="Wonderful World", name="Louis Armstrong", length="2:58">
文件 songdata 在同目录下,内容如下:
jazzj00132.mp3 345 Fats Waller Ain't Misbehavin'
jazzj00319.mp3 258 Louis Armstrong Wonderful World
bgrassbg0732.mp3 409 Strength in Numbers Texas Red
但是我运行的结果却是:
#<struct Song title=nil, name=nil, length=nil>
请问这是为什么呢?