B
Brad Ediger
ARGF.read[-128..-1].unpack("A3A30A30A30A4A30C")
Well played, sir. I always forget about ARGF. And to think I call
myself a Perl nerd.
-be
ARGF.read[-128..-1].unpack("A3A30A30A30A4A30C")
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(&methodrequire))
fields, genres = (Hpricot(open("http://www.rubyquiz.com/quiz136.html")) / "p.example").map{|e| e.inner_html}
fields = fields.split
genres = genres.split "<br />"
class NoID3Error < StandardError
end
class ID3
Genres=" Blues
Classic Rock
Country
Dance
Disco
Funk
Grunge
Hip-Hop
Jazz
Metal
New Age
Oldies
Other
Pop
R&B
Rap
Reggae
Rock
Techno
Industrial
Alternative
Ska
Death Metal
Pranks
Soundtrack
Euro-Techno
Ambient
Trip-Hop
Vocal
Jazz+Funk
Fusion
Trance
Classical
Instrumental
Acid
House
Game
Sound Clip
Gospel
Noise
AlternRock
Bass
Soul
Punk
Space
Meditative
Instrumental Pop
Instrumental Rock
Ethnic
Gothic
Darkwave
Techno-Industrial
Electronic
Pop-Folk
Eurodance
Dream
Southern Rock
Comedy
Cult
Gangsta
Top 40
Christian Rap
Pop/Funk
Jungle
Native American
Cabaret
New Wave
Psychadelic
Rave
Showtunes
Trailer
Lo-Fi
Tribal
Acid Punk
Acid Jazz
Polka
Retro
Musical
Rock & Roll
Hard Rock
Folk
Folk-Rock
National Folk
Swing
Fast Fusion
Bebob
Latin
Revival
Celtic
Bluegrass
Avantgarde
Gothic Rock
Progressive Rock
Psychedelic Rock
Symphonic Rock
Slow Rock
Big Band
Chorus
Easy Listening
Acoustic
Humour
Speech
Chanson
Opera
Chamber Music
Sonata
Symphony
Booty Bass
Primus
Porn Groove
Satire
Slow Jam
Club
Tango
Samba
Folklore
Ballad
Power Ballad
Rhythmic Soul
Freestyle
Duet
Punk Rock
Drum Solo
A capella
Euro-House
Dance Hall".split("\n").map{|x| x.gsub(/^\s+/,'')}
attr_accessor :title, :artist, :album, :year, :comment, :genre, :track
def genre_name
Genres[@genre]
end
def initialize(filename)
rawdata=open(filename) do |f|
f.seek(f.lstat.size-128)
f.read
end
tag,@title,@artist,@album,@year,@comment,@genre=rawdata.unpack
"A3A30A30A30A4A30c" if rawdata[3+30+30+30+4+28]==0
@track=rawdata[3+30+30+30+4+29]
@track=nil if @track==0
end
if tag!="TAG"
raise NoID3Error
end
end
end
You hard-coded the value of the unpack field.
What the heck is ARGF?Brad said:ARGF.read[-128..-1].unpack("A3A30A30A30A4A30C")
Well played, sir. I always forget about ARGF. And to think I call myself
a Perl nerd.
Johannes said:What the heck is ARGF?
Thank you.Joel said:It's a pseudo-IO that reads the concatenation of the files named in
ARGV, unless ARGV is empty, in which case it just reads standard input.
It's very useful in writing little command-line programs that can be
used as filters or on a list of named files (after you delete any
switches or options from the command line).
I've been extremely busy lately, but I wanted to give this one a try.
This solution is not complete as far as the problem specification
goes, but my bit o' metaprogramming-type stuff works, though I'd have
liked to push it further.
class ID3
@@recLen =3D 0
def ID3.field(name, len, flags=3D[])
I've been extremely busy lately, but I wanted to give this one a try.
This solution is not complete as far as the problem specification
goes, but my bit o' metaprogramming-type stuff works, though I'd have
liked to push it further.
This is a very clever solution. I have one suggestion though=85
class ID3
@@recLen =3D 0
def ID3.field(name, len, flags=3D[])
Changing flags=3D[] to *flags gives a nicer interface, I think.
I've been extremely busy lately, but I wanted to give this one a try.
This solution is not complete as far as the problem specification
goes, but my bit o' metaprogramming-type stuff works, though I'd have
liked to push it further.
This is a very clever solution. I have one suggestion though=85
class ID3
@@recLen =3D 0
def ID3.field(name, len, flags=3D[])
Changing flags=3D[] to *flags gives a nicer interface, I think.
True... I had thought of that this morning, though I also wanted to
add a conversion parameter... so a lambda or block could be provided
that would convert between the record's string data and an integer
(e.g. the ID3 year).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.