Cummings.P Cummings.P - 2 years ago 115
Ruby Question

Ruby Program not reading all lines in files

I am new to ruby and have the following code that reads in a file, then places the symbols separated by a '|' sign into different attributes of a Struc data type:

Song =, :name, :length)
song_file ="songdata.txt")
songs = []

song_file.each_line do |line|
file, length, name, title = line.chomp.split(/\s*\|\s*/)
songs <<, name, length)

p songs

The file songdata.txt contains the following txt:

/jazz/j00132.mp3 | 3:45 | Fats Waller | Ain't Misbehavin'
/jazz/j00319.mp3 | 2:58 | Louis Armstrong | Wonderful World
/bgrass/bg0732.mp3| 4:09 | Strength in Numbers | Texas Red

However, when I go to print the array songs, it only contains the first line of the text file. Any thoughts on to why this is?

Answer Source

The problem should be related to line endings. Ruby seems to handle Windows and Unix line endings all right, but would only read the first line when you change your file's line ending to OSX's ("Mac OS 9" in SublimeText).

Change line endings of your songdata.txt to Unix or Windows.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download