taglib-ruby を使用して、1 日に何回曲を聴けるかを調べています。単一の曲で動作するようになりましたが、ディレクトリをループして各曲の長さと1日に何回聞くことができるかを吐き出すことができるように修正しようとしています. 実行時にエラーはスローされませんが、何も出力されません。何らかの理由でファイルが表示されているとは思いません。foreach を使用しようとしましたが、次のようなエラーが発生し続けました。
$ ruby songtime.rb /media/ab/storage/Music/Between\ the\ Buried\ and\ Me/[2007]\ Colors/
songtime.rb:9:in `block (2 levels) in <main>': undefined method `length' for nil:NilClass (NoMethodError)
from /home/ab/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/taglib-ruby-0.7.0/lib/taglib/base.rb:8:in `open'
from songtime.rb:7:in `block in <main>'
from songtime.rb:4:in `foreach'
from songtime.rb:4:in `<main>'
次のようなディレクトリ名をプログラムにハードコードしただけでも、同じ問題が発生します。
#!/usr/bin/ruby
require "taglib"
Dir.foreach(ARGV[0]) do |songfile|
next if songfile == '.' or songfile == '..'
TagLib::FileRef.open(songfile) do |mp3|
properties = mp3.audio_properties
songLength = properties.length
puts "Song length is #{songLength}"
puts "You can listen to this song #{(24*60*60/songLength * 1000).floor / 1000.0} times per day."
end
end
だから私はグロブに切り替えてみました:
#!/usr/bin/ruby
require "taglib"
Dir.glob("#{ARGV[0]}*.mp3") do |songfile|
TagLib::FileRef.open(songfile) do |mp3|
properties = mp3.audio_properties
songLength = properties.length
puts "Song length is #{songLength}"
puts "You can listen to this song #{(24*60*60/songLength * 1000).floor / 1000.0} times per day."
end
end
これは機能しません。エラー メッセージはありませんが、何も出力されません。入れてもうまくいきません
#!/usr/bin/ruby
require "taglib"
Dir.glob("/media/ab/storage/Music/Between the Buried and Me/[2007] Colors/*.mp3") do |songfile|
TagLib::FileRef.open(songfile) do |mp3|
properties = mp3.audio_properties
songLength = properties.length
puts "Song length is #{songLength}"
puts "You can listen to this song #{24*60*60/songLength} times per day."
end
end
これでサップ?新人でごめんなさい。