Category Archives: Ruby

Scottkit is born! Only seven and a half years late!

Those of you who have been reading this blog since 2nd March 2010 — just three days after the blog was born — might remember the fourth post I ever made here: Learning a language vs. learning a culture, on my switching from Perl to Ruby. Way back then, I wrote about “ScottKit — my first non-trivial Ruby program”. Here it is!

Continue reading

We can’t afford to write safe software

Let’s start by thinking about a very simple example.  I’ve recently switched to using Ruby as my language of choice, after a decade as a Perl hacker.  Ruby does a lot of things more nicely than Perl, including having proper object syntax, simple everything-is-an-object semantics, a sweet module/mixin scheme and very easy-to-use closures, so I’ve mostly been very happy with the switch.  In so far as Ruby is a better Perl, I don’t see myself ever writing a new program in Perl again, except where commercial considerations demand it.

Continue reading

A hard-to-find bug, and equivalent APIs in low- and high-level languages

I just wrote an entry about a hard-to-find bug on the blog of my employer, Index Data.  It turned out to hinge on the different API expectations of programmers in low- and high-level languages — specifically C and Ruby — and I think it’ll be of interest to most Reinvigorated Programmer readers.  Enjoy!

Frameworks and leaky abstractions

A bit of personal history, as background, before we jump in …

In late 2009, I got it into my head that there was a web-site I wanted to build.  That web-site would be backed by a database, and would allow records in the database to be created, edited, listed, viewed, deleted and linked together in various ways.  (I won’t talk about the details now, because I want to save them for when I can link to the running site, which doesn’t exist yet.)

I’ve built a lot of conceptually similar sites in my time, mostly using Perl with HTML::Mason to embed mod_perl code fragments in HTML templates (kind of like the way PHP works).  This has entailed having some kind of layer between the application and the database to enable objects to be stored — what we now call an ORM layer, although I didn’t know that term back when I first wrote one in (probably) 2001.

Continue reading

Early experiments with JRuby

I’ve made no secret on this blog that I am fond of Ruby, and that I find Java rather verbose and awkward.  But at the same time, I realise you can’t deny the commercial importance and ubiquity of Java.  I’ve been hoping to find a way to combine the stronger points of both languages, and of course JRuby gives me just what I need.

So I’ve been experimenting with it for the last few days, and I like what I see.

For example, whereas in vanilla Ruby you might write:

def qsort(a)
return a if a.length <= 1
pivot = a.delete_at rand(a.length)
return (qsort( { |x| x <= pivot }) +
[ pivot ] +
qsort( { |x| x > pivot }))

In JRuby, you simply write:

jrequire ‘java.lang.Jruby.*’
jrequire ‘java.lang.util.ArrayList’
def public ArrayList<T> qsort<T> (theArray is ArrayList<T>)
if Arrays.lengthOfArray(theArray) <= 1
return theArray

theRandomInteger = RandomIntegers.rand(theArray.length)
pivot = Arrays.elementAtIndex(theArray, theRandomInteger)
Array.deleteElementAtIndex(theArray, theRandomInteger)

lowerTemporaryArray = ArrayList<T>.new
Iterator<T> iter = theArray.iterator()
while iter.hasNext()
theValue =
if theValue <= pivot
lowerTemporaryArray = qsort(lowerTemporaryArray)

upperTemporaryArray = ArrayList<T>.new
iter = theArray.iterator()
while iter.hasNext()
theValue =
if theValue > pivot
upperTemporaryArray = qsort(upperTemporaryArray)

resultArray = ArrayList<T>
Arrays.appendElementsOfArray(resultArray, lowerTemporaryArray)
Arrays.appendSingleElement(resultArray, pivot)
Arrays.appendElementsOfArray(resultArray, upperTemporaryArray)

return resultArray

It’s an important step forward.  I think I’m going to be using it a lot.

So what actually is my favourite programming language?

The recent series of Why XYZ Is Not My Favourite Programming Language articles has been fun to do, and it’s been great to see the discussion in the comments (even if it’s mostly people people saying that I am talking a load of fetid dingo’s kidneys).  But I don’t want to extend that series beyond the point of diminishing returns, and it’s time to think about what it all means.  As duwanis commented on the Ruby article, “I’m a bit lost as to the point of these posts”; now I want to try to figure out just what, if anything, I was getting at.

Continue reading

Why Ruby Is Not My Favourite Programming Language


There’s one wrinkle when while and until are used as statement modifiers. If the statement they are modifying is a begin/end block, the code in the block will always execute at least one time, regardless of the value of the boolean expression.

— David Thomas and Andrew Hunt, Programming Ruby: The Pragmatic Programmer’s Guide, section Expressions.

And the proof:

$ cat crud.rb
print "Hello\n" while false
 print "Goodbye\n"
end while false
$ ruby crud.rb

Case closed.

Learning a language vs. learning a culture

[This post is partly recycled from my recent not-really-on-topic entry on the Index Data blog.  Apologies to anyone who’s seen the earlier version.  If that’s you, skip down to ScottKit — my first non-trivial Ruby program, just after the sushi: it’s all  new material from there on.]

Out with the old, in with the new

A month or so ago, it suddenly struck me that I am writing most of my programs in the same language (Perl) that I was using a decade ago. Sure, I’ve learned and used some other languages since then, notably Java and JavaScript, but neither of those has wired itself into my hindbrain the way C did in 1986 and Perl in 2000, so that they spring unbidden to my fingertips when I open a fresh emacs buffer to start a new program. Ten years is a long time to ride the same horse, especially when programming environments are moving so fast, so I decided to make a conscious effort to learn something new. Partly just to keep myself sharp; partly because learning a new language is learning a new way of thinking, which is useful when programming in an old language, too; and partly in the hope that I might find something that is just plain Better than Perl.

The main cast of Buffy

Main cast of Buffy the Vampire Slayer, season 2.  Left to right: Willow, Oz (seated), Giles, Cordelia, Xander, Buffy, Angel, Spike (seated), Drusilla.  I admit this is not realated to my subject, but it does add visual interest.  They say that’s important.

Continue reading

Closures, finally explained!

Programming language weirdos (by which I mostly mean people who program in Lisp) are forever telling you that you need closures, and that your language is unbearably primitive if it doesn’t support them.  Turns out that they’re right, but their rightness is completely obscured by the horrible, uninformative term “closure”, and even more so by the fact that the definition given for the term is usually some opaque hunk of verbiage like “a first-class function with free variables that are bound in the lexical environment” (from the Wikipedia article on the subject.)

It also doesn’t help that the examples people usually give are so lame: it’s all “Here’s a function that returns a function that yields the value of a hidden variables that is incremented every time it’s called”.  Whenever I see examples like that, I just think “Why would I ever want such a thing”?

Continue reading