ruby rename elements of array with elements of different array -
i have file looks this:
ttitle0=track name 1 ttitle1=track name 2 and directory contains track01.cdda.wav.mp3 , track02.cdda.wav.mp3
i have following code, creates 2 different arrays, 1 track names , 1 track titles:
tracks = dir.glob("*.mp3") tracknames = array.new file.open('read').each |line| if line =~ /ttitle/ tracknames << line.split("=")[1].strip! end end this gives me 2 arrays:
["track name 1", "track name 2"] and
["track01.cdda.wav.mp3", "track02.cdda.wav.mp3"] i rename files in second array elements of first array. so, "track01.cdda.wav.mp3" become "track name 1.mp3".
here have tried far:
tracks.map {|track| file.rename("#{tracks}", "#{tracknames}.mp3") } and error:
no such file or directory - track01.cdda.wav.mp3track02.cdda.wav.mp3 or track name 1track name 2 (errno::enoent) i have keep in mind in future there number of elements in each array, numbers equal each other.
any ideas?
use array#zip:
tracks.zip(tracknames).each |track, trackname| file.rename track, "#{trackname}.mp3" end alternatively (less fun, doesn't create intermediary array of arrays prior enumeration):
tracks.each_with_index |track, i| file.rename track, "#{tracknames[i]}.mp3" end
Comments
Post a Comment