elfs: (Default)
[personal profile] elfs
I don't get many chances to use Ruby, but when I do I remain breathlessly impressed with it. Tonight, I had to write a program to extract the XML meta-data from a mass collection of documents (The Journal Entries, naturally) and process that meta-data into fields, which would then get output as a CSV so I could edit it with a spreadsheet. One of the tricky parts was putting the filename, which is not part of the embedded metadata, as the first column in the CSV.

I did it in one line. Not a Perl line where, a month from now, I'll have no idea what the heck I was writing. One readable, understandable, clear and concise line.

Oh, I so like this language. I wish I had more opportunities to use it.

Date: 2006-09-01 07:48 am (UTC)
From: [identity profile] sarekofvulcan.livejournal.com
Drat.

Thanks so much, Elf, now I have another language to learn....

Date: 2006-09-01 11:54 am (UTC)
From: [identity profile] sandhawke.livejournal.com
Can you post that line for us?

Date: 2006-09-01 01:15 pm (UTC)
From: [identity profile] elfs.livejournal.com
print Dir['temp/*.xml'].collect {|x| x.sub('temp/','')}.collect {|f|
["title", "arc", "pubdate", "wordcount", "cookie", "wikipage", "storycode"].collect {|c|
XPath.first(Document.new(File.open('temp/' + f, "r")), '//' + c).text || "n/a"}.unshift(f).join("\t")
}.join("\n")


Looking at it twice, maybe it's not so readable in the morning. But I can start to puzzle out the intention and arrive at understanding much quicker than I could with Perl.

Profile

elfs: (Default)
Elf Sternberg

December 2025

S M T W T F S
 12345 6
78910111213
14151617181920
21222324252627
28293031   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Dec. 27th, 2025 04:07 pm
Powered by Dreamwidth Studios