Best when chunky and delivered by foxes

Hooray for bacon! [ffffound]

Parsing Expression Grammars in Ruby with Treetop

A couple of years ago I read and greatly enjoyed Bryan Ford’s 2004 paper “Parsing Expression Grammars: A Recognition-Based Syntactic Foundation” [PDF, 134Kb] (wow, it’s still on my desk, in fact); while to my disappointment it proved inapplicable to my research (I’m constrained in the parsing and language description technologies I can use by what I have to integrate with), it was clearly Cool Stuff.

Yeah, so, apparently some Ruby hacker read that paper too [reddit].

A lucid explanation of closures

Here’s a pretty clear explanation [reddit] of closures, which have hit the mindspace in a big way since Rails made Ruby the hot sauce of the day. When I say “clear”, I mean, perhaps, from a “traditionalist imperative” point of view — the discussion of stack frames and the funarg problems in particular. Of course, lambda-heads and category theorists probably have other criteria for “clear”. ;-)

Rails vs Django

Rails vs Django [smallcool].

Interesting and balanced. I tried Django about a year ago and did indeed get going with it quite quickly, although the lack of migration was a big pain in the butt, and sounds like a killer feature in Rails.

Tail call optimisation at Raganwald

I command all of my students reading this to go and read this piece on tail call optimisation. If you’re anything like me, you’ll need to read it at least twice, write out the ruby examples by hand so they actually enter your brain (maybe I just needed more coffee), and follow many of the links for further explanation. It will be well worth it. This man has intelligent things to say, with which it is worth being familiar, if not intimate.

“Ridiculously easy” distributed programming with Ruby

MapReduce for Ruby: Ridiculously Easy Distributed Programming [raganwald]

Great for tasks which trivially parallelise, anyway.

wmii FreeBSD and xmms plugins (in ruby)

A few days ago I started using wmii, with Mauricio Fernandez’ ruby scripting magic, and oh it’s fun.

The status bar monitors that come in Mauricio’s code don’t work for me, however, because I’m running FreeBSD not Linux, and things like uptime and battery status get report differently. Also, there wasn’t any xmms control/monitoring. I have now fixed both of these problems, and invite others to partake of the goodness.

So: wmii-freebsd@gimbo.org.uk.rb – a ruby/wmii plugin defining status bar monitors which work under FreeBSD.

And: wmii-xmms@gimbo.org.uk.rb – a ruby/wmii plugin defining an xmms status bar monitor, and some key bindings for simple control of xmms (play/pause; next; previous; forward 5 secs; back 5 secs; toggle shuffle; er, that’s it).

Status bar screenshot:

ruby wmii freebsd/xmms status bar monitors

From left to right: xmms monitor displaying track name, time elapsed, shuffle status (“>” is normal, “@” is shuffle); current master volume; the “-N-” is a standard plugin so I say nothing here; temperature and CPU speed; load averages; uptime (h:mm); battery status and time remaining; date/time.

Comments, suggestions, bugfixes, criticisms of my appalling ruby code all welcome.

wmii is better than ion

In August 2002, I started using ion as my window manager (and last year, upgraded to ion3). Today, I stopped using it.

ion beats the crap out of conventional window managers: placing and resizing windows is so tedious (not to mention 20th century ;-) ). It’s lightweight, maximises screen real estate, and has great keyboard support, all of which appeal to me. Until today, I’d have recommended it to anybody.

I did, however, have the following problems with ion: 1) Lua: yick and oh my god, yuck. What an awful language, but you’re stuck with it for configuration/control. 2) The documentation: there, but not very helpful. Too referencey, too automatically produced. 3) Tuomo (the author): sorry, but that is one surly gringo, and heaven help you if you disagree with him. None of these are killers (except maybe lua), but they’re the reasons I’m happy to leave ion behind.

Thus, introducing wmii, which is superficially similar to ion but has a number of features which really set it apart. The most important is its Plan9-inspired approach to control, which allows any language for configuration. You know what’s coming next? Ah yes, we can configure & control wmii using ruby [via the immortal _why].

There are other good reasons to use wmii, but that one is probably sufficient for me – and it speaks of a thoughtful and open design which can only bode well. The wmii codebase really is tiny, by the way – it compiled in no time at all. I started using it this afternoon and I don’t see any reason to stop: it’s easy enough to pick up, although I’ll be tweaking the config for the next week or so. Using Ruby – w00t!

Understanding Ruby blocks, Procs and methods

Understanding Ruby blocks, Procs and methods.

Scripts in ruby a la python’s __name__ == ‘__main__’ idiom

A common idiom in python is to check the special variable __name__ to see if the current module is being run as a script or not. For example:

class Foo:
    ...

def bar():
    ...

if __name__ == '__main__':
    bar()

Here, if the module is run as a script (ie passed directly to the python interpreter), then __name__ has the value “__main__”, this is detected, and (in this case) the bar() function is called. On the other hand, if the module is just imported from some module, __name__ has a different value (the name of the module file, I think?), and bar() doesn’t get called.

This is nice for a number of reasons – for example, you might put unit tests into bar().

How to do this in Ruby? It’s not in FAQ, which surprised me. I was about to ask on ruby-talk but then remembered the biggest FAQ of them all, and turned to google. Aha (and eek, what a horrible mailing list interface). Anyway, it’s:

if __FILE__ == $0
    bar()
end

OK, so why does this work?

$0 contains the name of the script being executed – ie, the name of the file that was passed to the interpreter. Whatever code you’re executing, this value never changes over a particular run of ruby. On the other hand, __FILE__ is always the name of the current source file. If the two match, then the current source file is the one that was passed to the interpreter.

I guess that’s pretty clear. Cool.

Welcome to tenthousandfactorial.com

Gimboland continues to turn into www.tenthousandfactorial.com… Peter William Lount, editor of www.smalltalk.org shows us how to do it in Smalltalk. Apparently Smalltalk’s been able to do this since about 1980. When I did my first degree, the boys doing straight Computing (I was doing Computing & Pure Maths, not gay Computing, by the way) met Smalltalk as their first O-O language, but alas I never had any serious exposure. The main impression I was left with was of every Smalltalk program being somehow an image of an entire virtual machine, which seemed odd and limiting but was probably neither (this was ten years ago, btw). *Shrug*

Moving on a few years, it had to happen, Daniel Berger wrote and told me how to do it in Ruby. He say:

Hi, I thought this might interest you. Here’s a “factorial” method in Ruby:

class Integer
    def factorial
        (1..self).inject { |f, n| f * n }
    end
end

… and, with that in place, we can now do this:

10.factorial -> 3628800
10000.factorial -> really big number

Or, you could just do that as a standalone method, rather than defining it within the Integer class like so:

(1..10).inject{ |sum,n| sum * n } -> 3628800

Yes, Ruby handles bignums “out of the box”. :) Regards, Dan

Fair enough, though to my mind this is at least as esoteric as the Pythonic reduce-based method mentioned previously. Again, provided you know the semantics of insert, I guess it makes perfect sense. For my money the Haskell version is still the nicest, just because it’s closest to how a mathematician would naturally go about defining factorial. Smalltalk comes close – it looks the part, but there’s still some odd syntax there, IMHO.

This is great. What started out as a glib comment which at the back of my mind I thought was probably wrong but hey, no-one ever reads Gimboland so what the heck, has turned into a nice little thread… :-)

Any other takers?