regex - Ruby method undefined for nilClass errors for string methods -
i don't understand why below doesn't work:
#todo: #open file-io-samples/multiplelinescustomdelimiter.txt #break each line fields #convert times mm:ss seconds #remove redundant spaces. ####################################################### song = struct.new(:title, :name, :length) file.open("file-io-samples/multiplelinescustomdelimiter.txt") |file_data| songs = [] file_data.each |line| file, length, name, title = line.chomp.split(/\s*\|\s*/) name.squeeze!(" ") #error: undefined method nilclass mins, secs = length.scan(/\d+/) #error: undefined method nilclass secs += mins*60 songs << song.new(title, name, secs) end puts songs[1] #make sure output consistent. end
contents of file that's being read:
/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 : : : :
this example programming ruby book. dont know why scan method , squeeze method throw errors when string objects not nilclass objects. without these output puts songs[1] looks correct.
the last row
: : : :
doesn't match split, name, length , title nil
file ends
: : : :
a parallel example:
a,b,c = [1,2] #a=1 #b=2 #c = nil
Comments
Post a Comment