What is The Ruby Way?

General 3 October 2009 | 2 Comments

The Ruby Way (buy it at Amazon.com) is a Ruby programming book published by Addison-Wesley that’s often considered the “bible” of Ruby books. It covers a dizzying amount of Ruby topics in a “how to” format – including low level data types, metaprogramming, I/O, graphics libraries, Web development tools, OOP, IDEs, and more (see the full table of contents). It’s an essential book for any Rubyist’s library.

Where To Buy

Amazon typically offers the best price for The Ruby Way. Check it out on Amazon.com (US) or Amazon.co.uk (UK).

A Ruby Idiom: Defining Methods Based on Conditions at Runtime

General 5 October 2009 | 0 Comments

On pages 36 and 37 of The Ruby Way (2nd Edition) Hal talks about “Coding at Runtime.” He specifically mentions how while ifdef directives can be used in C to change how things are defined depending on conditions set prior to the compile, you can just do the same at runtime in Ruby.

For example, let’s say your app is designed to run on multiple platforms, but a certain action has to be executed in a totally different way for a couple of platforms (a common situation where Windows is involved). You could define your logic like this:

if platform == Windows
  action1
elsif platform == Linux
  action2
else
  default_action
end

To this sort of arrangement Hal says: “Of course, there is a small runtime penalty for coding in this way since the flag may be tested many times in the course of execution.” He then presents a technique where a method itself is defined at run-time based upon the condition:

if platform == Windows
  def my_action
    action 1
  end
elsif platform == Linux
  def my_action
    action2
  end
else
  def my_action
    default_action
  end
end

To this, Hal says: “In this way, the same result is achieved, but the flag is only evaluated once; when the user’s code calls my_action, it will already have been defined appropriately.”

Personally I’m not entirely convinced by Hal’s specific example in The Ruby Way, but it’s a handy reminder of how dynamic Ruby is. I’m sure there are some great examples of this technique in the wild.

Basic Ruby Idioms: Checking For An Empty String

Data, General 4 October 2009 | 0 Comments

Page 15 of The Ruby Way (2nd edition) presents a basic Ruby idiom for checking whether a string is empty or not.

I believe there are three ways to do it (if you can think of any others, leave a comment!). Two reasonably obvious, but one that’s quite idiomatic (the one that The Ruby Way mentions):

  1. str.empty? – With this we simply ask a String object if it’s “empty.”
  2. str.length > 0 – With this we check whether the string has a length of more than zero – i.e. it’s not empty.
  3. str[0] – With this we check the first character of the string. If no first character is present, both Ruby 1.8 and 1.9 return nil! This is particularly idiomatic and not something I’d considered before.

Of course, the shortest or most idiomatic route is not usually the best way to go when programming, but if you’re playing code golf, doing some obfuscation, or otherwise want to access the first character of the string anyway, option #3 is undeniably sneaky.

Note: Remember that while a string can only be empty or non-empty, a variable that you think is referring to a String object may well be nil! So often you need to check for nil as well as string length. You can do this with a simple if str or if str.nil?

Kernel.abort: Quit and Print an Error to STDERR

General 4 October 2009 | 0 Comments

One of the great things about going through other people’s Ruby books is that you discover small tidbits about the language that you’d never noticed before. While skimming through The Ruby Way again to come up with ideas for this blog, I noticed a basic program on page 14 (2nd edition) that starts like so:

print "Please enter a temperature and scale (C or F): "
str = gets
exit if str.nil? or str.empty?
str.chomp!
temp, scale = str.split(" ")

abort "#{temp} is not a valid number." if temp !~ /-?\d+/

The abort line threw me for a loop; I’d not seen it in use before. I jumped into irb to give it a try and, yes, it works and, yes, it’s documented:

abort.png

Writing to STDERR can be pretty handy, especially if you’re a UNIX nerd threading together apps at the command line. So.. a nice find, even on such an early page of the book.