More Poignant Guide Shenanigans

I’m still loving the Poignant Guide to Ruby, but having to figure out errors in the code is a bit of a pest. Not sure if they are mistakes, or if the language has evolved and left behind a once-correct guide to get out of date. The problems come in Chapter 5. Where the whole ‘lottery machine’ scenario is being laid out, I found a couple of issues that stopped the code from running:

  • The << notation, for “adding directly to the definition” of an object, and thereby creating a class method instead of an instance method, does not seem to work in the way Why describes it… in fact it throws up this error:
 warning: class variable access from toplevel singleton method
 in 'play': uninitialized class variable @@tickets in Object (NameError)
  • This can be fixed by extending the class normally, without the <<, then declaring the method as def LotteryDraw.play or def self.play, both of which will force it to be a Class method. The first two lines should read like so:
 class LotteryDraw
  def self.play
  • Secondly, some errors occur in concatenating integers with strings, resulting in this error:
in '+': can't convert Fixnum into String (TypeError)
  • One fix for this is to call the method ‘to_s’ on each of the integer values, which seems to be an equivalent to .NET’s .ToString() method. Not sure if it is the ‘most correct’ solution in the context of the puts method, but it seems to work for now…
LotteryDraw.play.each do |winner, tickets|
  puts winner + " won on " + tickets.length + " ticket(s)!"
  tickets.each do |ticket, score|
    puts "\t" + ticket.picks.join( ', ' ) + ": " + score.to_s
  end
end

A forum post here seems to suggest similar solutions, and is honoured with a response from Why himself, although it’s not clear that the guide has been maintained since then. It does make me wonder, if the guide is published under Creative Commons, does that allow the community to re-publish a corrected version even if the original author has better things to do with his time?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: