0

次のような音声タグがあります: " 00:14:164"、つまり " MM:SS:splits"。

形式を数値にして、秒単位のみにしたいと思います。

たとえば、次のようになります。

" 00:14:164" だろう14.164

" 01:59:582" だろう119.582

これを行う最善の方法は何ですか?

4

1 に答える 1

2
def tag_to_f(tag)
  a = tag.split(':').map(&:to_i)
  "#{a[0] * 60 + a[1]}.#{a[2]}".to_f
end

tag_to_f "01:59:582"

またはモンキーパッチString

class String
  def to_f_with_audio_tag
    if a = match(/^(\d+):(\d+):(\d+)$/).to_a[1..3].map(&:to_i) rescue nil
      "#{a[0] * 60 + a[1]}.#{a[2]}".to_f_without_audio_tag
    else
      to_f_without_audio_tag
    end
  end

  alias_method_chain :to_f, :audio_tag
end

"01:59:582".to_f
于 2012-06-14T03:49:49.087 に答える