My Year II students are currently learning scripting with Ruby, my favorite programming language (ex aequo with Scheme I think…). Today I thought about what could be the difference between scripting and programming and I came up with:
Scripting is the same as programming except that you don’t have to worry about unnecessary details like deciding upon types, declaring classes and handling exceptional conditions. In other words, you only focus on the fundamentals.
Ruby is an excellent scripting language because it has been designed correctly. My students have had no problems understanding Ruby code that they were seeing for the first time. In fact, Ruby is a very elegant language.
Ruby is also very powerful thanks to the predefined functionalities in Ruby Core and the Ruby Standard Library. I have naturally spent some time explaining the likes of strings, regular expressions, etc. to my students but also libraries like OpenURI (used to access resources likes files, images and web services found on the Internet) and REXML (used to extract parts of an XML file.)
Consider the following Ruby script for instance:
require "open-uri"
require "rexml/document"Â Â Â
artist = gets
artist.chomp!
artist.gsub!(" ","+")
input = open("http://ws.audioscrobbler.com/1.0/artist/#{artist}/toptracks.xml")
doc = REXML::Document.new(input)
puts
puts "The 10 greatest tracks are:"
puts
count = 1
doc.elements.each("mostknowntracks/track") { |track|
  puts "(#{count}) #{track.elements["name"].text}"
  count += 1
  break if count > 10
}
This simple Ruby script prompts the user for the name of an artist and queries Audioscrobbler for his/her top music tracks using OpenURI. The XML file returned is parsed using REXML and the top 10 tracks are displayed on screen. This is what I get when running it:
$ ruby top-10-tracks.rb Depeche Mode The 10 greatest tracks are: (1) Enjoy the Silence (2) Personal Jesus (3) Precious (4) Just Can't Get Enough (5) Policy of Truth (6) Never Let Me Down Again (7) It's No Good (8) Dream On (9) Strangelove (10) World in My Eyes
and also
$ ruby top-10-tracks.rb Avinash Meetoo The 10 greatest tracks are: (1) This is real (2) Play (3) Beautiful sea (4) Helium (5) The myst (6) Zygma (7) Seduction (8) Deception (9) Eternity (10) Lion Heart
Not bad for a 20-lines long script! Incidentally, I deeply believe that Ruby is a great language to learn programming.
This is a great quote from one of my heroes, Mr Steve Yegge!
Ruby‘s one of the easiest languages for just about anyone to learn — it’s maybe a 3-day exercise, and after a week or so of using Ruby, you just may never look back. It’s like getting a new car: it still gets you from A to B, but without your old car’s flaky transmission, cracked windshield, and 5-year-old french-fry remnants under the driver’s seat.
Who am I to argue?