Category Archives: Ruby

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(a.select { |x| x <= pivot }) +
[ pivot ] +
qsort(a.select { |x| x > pivot }))
end

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
end

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 = iter.next
if theValue <= pivot
lowerTemporaryArray.append(theValue)
end
end
lowerTemporaryArray = qsort(lowerTemporaryArray)

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

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

return resultArray
end

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

Ahem.

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
begin
 print "Goodbye\n"
end while false
$ ruby crud.rb
Goodbye
$

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