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

Advertisements

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.