Saturday, January 28, 2006

Technical - "Programing in Ruby"

This is another book from The Pragmatic Programmer publishers. This is a pretty thick book, which includes an introduction to Ruby as well as a long library reference. I've read some of the introductory chapters, but I use it mostly for reference while hacking some Ruby code.

One early program I wrote was to help my friend Jack download a bunch of MP3 files that were refernced by a specific web page. Here is the code:

require "net/http"
require "open-uri"

def get_page (uri)
# split into parts
uri =~ %r{http://((\w|\.)*)}
@@domain = $1
file = $'
# connect to the web server and fetch the page '
puts "-->> Connecting to domain #{@@domain}\n"
#"
result = ""
Net::HTTP.start(@@domain,80) {|http|
response = http.get(file)
result = response.body
}
result
rescue
puts "Unable to open #{uri}"
end

def download_mp3 (base_uri, file)
u = base_uri + file
# Replace blanks by _ in file name
fname = file.gsub("%20", "_")
if (File.exists?(fname))
puts "--> Already have #{fname}"
else
puts "--> Saving <#{fname}>"
f = File.open(fname, "w");
u = OpenURI.open_uri(u)
f.write(u.read)
end
rescue
puts "Unable to save #{fname}"
end

if (ARGV.length == 0)
puts "Usage: get_mp3.rb "
else
base_uri = ARGV[0]
page = get_page(ARGV[0])
count = 0
page.scan(%r{<a href=((.)(.)*\.mp3)}) {
download_mp3(base_uri, $1[1..-1])
count += 1
}
puts ">>> Downloaded #{count} files...\n"
end


This program probably could be made shorter, but it does work. I had fun trying to come up with proper regular expression.

This book is "the" Ruby reference at this point and I have so far only scratched the surface. Meanwhile I'm working on another project with Ruby and Ruby on Rails - a program to index MP3 files.

Science Fiction - "The Futurological Congress"

This is another one of Stanislaw Lem's Ijon Tichy stories. In this one Ijon Tichy attends a Futurological Congress in Costa Rica. While there a revolution breaks out and various hallucinogenic chemicals are used to subdue the revolting population. As Tichy is exposed to various chemicals, after a while we cannot tell what is imaginary and what is real. There are walking rats, rockets packs etc.

Eventually it appears that Tichy is frozen and revived in a future (year 2039) world where everything is controlled by psychem. There are drugs for everything. If you need to feel happy you take a pill - no one dares to have un-chemical emotions.

The funny and frustrating thing about this book that when describing the world of the future, Lem goes insane with making up new words. Some of these are obvious puns or somewhat logical names for new drugs, other are completely arbitrary.

At one point in the story Ijon Tichy discusses how futurology operates in this world with a professor of the subject. The way futuroligists work is to randomly generate new words and then attempt to assign meanings to them. This got me thinking about how language changes due to technological changes, and how incomprehensible it would be to someone from the past.

What would you think when someone told you 30 years ago: "If you want to hear the song, just google the lyric, download the mp3 and put it on your iPod".

Sunday, January 08, 2006

Non-fiction - "Unweaving the Rainbow"

"Unweaving the Rainbow" is a collection of essays by Richard Dawkins, subtitled "Science, Delusion and the Appetite for Wonder". The book was prompted by a poem (I think is was by Keats) which complained that Newton, by unweaving the rainbow (that is by splitting the light) somehow reduced the beauty of the natural world. Dawkins argues the opposite - the scietific explanations give us much deeper and much more wonderous view of the world. In fact there is a quote at the start of the first chapter from Mervyn Peake: "To live at all is miracle enough".

The various essays cover some implication of spectrum splitting - for light and for sound. The author provides very nice explanations of how we know the composition of stars, based on analysis of star light.

There is an essay, called "Barcodes at the Bar", which explains in detail how genetic testing is used in criminal investigations. It turns out that your genetic fingerprint is based on the DNA that lies between the genes that specify your body. The strings of junk DNA are unique for each individual. Even so, genetic testing can sometimes give a false positive. This has to do with how these tests are performed, since it is clearly inpractical to compare person's entire genome.

Several of the essays talk about debunking pseudo-science. In particular he explains the probablities behind coincidences.

I've been working through this books slowly, as I get distracted by other books. Some of the debunking stuff I've read about elsewhere, and after a while it becomes little boring to read.

Still, this is a nice book to give to someone who wants to learn more about what science is really about.