On Mon, 27 Aug 2007 02:32:05 +0900, Brad Ediger wrote:

> One of the biggest problems in software development is feature creep. In
> the case of this Quiz, specification creep was the culprit, with the
> spec being changed two times in two days. No offense intended, JEG2 ;-)
> 
> Luckily, we can use the mighty power of Ruby to make our application
> impervious to such changes, and save a couple heredocs to boot.
> 
> -------------------------
> 
> #!/usr/bin/env ruby -rubygems
> 
> %w(hpricot open-uri).each(&method(:require))
> 
> fields, genres = (Hpricot(open("http://www.rubyquiz.com/quiz136.html")) / "p.example").map{|e| e.inner_html}
> fields = fields.split
> genres = genres.split "<br />"

You hard-coded the value of the unpack field. If you wanted to download the spec 
properly, you'd generate that from the spec like follows. (Picking up from the end
of what I've quoted above)

unpacktypes=Hash.new("A30")
unpacktypes["TAG"]="A3"
unpacktypes["year"]="A4"
unpacktypes["genre"]="c"
unpackstr=fields.map{|x| unpacktypes[x]}.join

id3=Hash.new
raw=open('/home/bloom/scratch/music/rondo.mp3') do |f|
  f.seek(f.lstat.size-128)
  f.read
end

values=raw.unpack(unpackstr)

fields.zip(values).each do |field,value|
  id3[field]=value
end

fail if id3["TAG"]!="TAG"

if id3["comment"].length==30 and id3["comment"][-2]==0
  id3["track"]=id3["comment"][-1]
  id3["comment"]=id3["comment"][0..-2].strip
end

id3["genre"]=genres[id3["genre"]] || "Unknown"
p id3

-- 
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/