ruby

Maybe Method for Ruby

Sometimes, when working in Ruby on Rails, I find myself writing code like:

foobar = FooBar.find(:first)
default = foobar.name if foobar

In Haskell, this would be a classic usage case for the Maybe monad. In Ruby, we don’t quite have the capabilities to make it implicit, but we can approximate the behavior explicitly:

class NilClass
  def maybe(*a); self; end
end
 
class Object
  alias :maybe :send
end
 
default = FooBar.find(:first).maybe(:name)

A small hack perhaps, but it saves a bit of typing.

PIM Idea for Command Line Junkies

I’m not a very organised person. I try to be, but it takes a lot of effort, and, being a programmer, I’m a naturally lazy sort of person. Whenever there’s a repetitive problem, I lean toward automating it in the most generally applicable way possible.

There are any number of tools for organising information, but they all tend to be either highly specific, such as a todo list, or very difficult to automatically organise and sort, such as a wiki. Ideally, I’d like a system that gives me the best of both worlds; the ability to restrict data to a specific form, but a form of my choosing. Essentially, I want a database.

However, most databases aren’t designed for quick, user-friendly, interactive use; that’s why we spend so much effort putting pretty frontends on them. Unfortunately, these facades often limit what we can do. Once we wrap our database in a GUI, we can no longer execute abitrary queries to pull out the data we want.

So what I really want is an interface that allows me to retain all the flexibility of the underlying database. In other words, I want a CLI, a REPL, an interactive prompt which I can use to query and build my database. And for this I need a query language that is both concise and succinct, something I can use to create queries in seconds, and something whose entire syntax can fit on the back of a postcard.

Network Monitoring in wmii

I’ve recently been playing around with wmii, a very minimal window manager for Linux. It uses a virtual filesystem to allow third party scripts to interact with it, in the style of procfs or Plan 9. ruby-wmii is just such a system for Ruby, and I’ve been spending an interesting time trying it out.

I’m not sure whether I like wmii yet, but I’m willing to give it a chance. It’s very minimal, but it’s nice to be able to have windows automatically tile, and I rather wish there were some way of doing it in GNOME or KDE. Maybe I’ll come to like the minimalism as well, but for now it seems there are a few things missing that could be rather useful.

One of these useful but missing things is a way to monitor network traffic, but it’s not too hard to add it. I’m just surprised that someone hasn’t created a plugin for it already. Briefly, then, here’s the class I use to poll for traffic information…

Syndicate content