I have now waded through the Ruby chapters and exercises of the 7lang7weeks -book. A sparkle has been lit I must say. Ruby does seem to be a very productive programming language, ideal for quick development, prototypes and for situations where time-to-market is essential. Just like Perl. Although I would not compare these two languages as they belong to somewhat different problem domains. They are similar in many good ways.

I found pp, a pretty printer for Ruby data structures, lot in vein of Perl’s Data::Dumper. It is not as good at first glance but my experience with is still in its infancy.

The idea of open classes is marvelous although it requires some discipline from the developer since dynamic typing allows the usage of methods and functionality that might be defined somewhere else. The compiler really can’t know it yet. Improper usage will result in run-time errors. This is one of the major drawbacks of ducktyping. It gives you freedom but with that freedom comes responsibility. Don’t ignore it.

I fiddled a little with modules, mixing functionality into other classes. This is one of the experiments

module Tweetify
  def tweet(str)
    puts "[Tweet!] " + str
  end
end
 
class Speaker
  include Tweetify
  attr_accessor :voice, :s
 
  def initialize(voice)
    @voice = voice
    @s = {
      'loud' => lambda { |str| puts str.upcase! },
      'low' => lambda { |str| puts "[shhh!] " + str.downcase! },
      'default' => lambda { |str| puts str }
    }
  end
 
  def speak(what)
    @s[@voice].call(what)
  end
end
 
s = Speaker.new("loud")
s.tweet("twit twit")
s.speak("am I yelling?")
s.voice = "low"
s.speak("I SHOULD BE WHISPERING NOW")

I learned through a struggle that instance variables must be initialized within a method, otherwise their content is lost. Or I just don’t know the intricacies of Ruby well enough. Overall the books Ruby exercises are good. Exercises touch the parts that Ruby is good at. Something more difficult could be useful and something to be done from the scratch instead of extending the books code snippets.

Now it is time to move forward to the next language (io!) but I would love to stay with Ruby a little longer. I just may have to find something for my own pleasure. Stay tuned!

Post filed under 7 languages, Software Craftsmanship and tagged , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *