waffle

Serv

The next episode in my “Dicking around with Ruby” series: Serv.

Serv is a very basic TCP server with a twist: it handles the footwork of waiting for lines and handling disconnections and multiple clients, and you provide the hooks for “when stuff happens”. It won’t do binary protocols, it won’t do protocols that don’t use line breaks everywhere and it won’t do UDP. However, I do believe it could be used to implement, say, an IRC server or an HTTP server.

For example:

require 'socket'
require 'serv'

serv = Serv.new(4242)

serv.add_hook(:introduce, proc {|par| 
    sock = par[:sock]
    sock.puts "Hey there! It's #{Time.now.utc.strftime('%H:%M:%S')}" +
        " UTC. Do you know where your children are?"
    serv.hdo :revoke, sock
})

serv.run

The socket guts of Serv is based on a short example put forward in Sockets programming in Ruby, although there’s not a lot that could be done differently, as evidenced by a quick search for similar examples. For this reason I release Serv into the public domain. No license.

Here’s that download link again.

Thoughts?

Each

If there’s one thing I love about Ruby (beyond its design overall), it’s definitely iterators. Martin “fitting last name” Traverso implemented iterators in Java for fun some 19 months ago (yes, I’m fast) and here’s how they roll:

EnumerableCollection<Integer> list = new EnumerableArrayList<Integer>();

list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);

list.each(new IterationCallback<Integer>() {
     public void call(Integer element) {
         System.out.println(element);
     }
});

That’s a lot of typing, and I don’t mean on a keyboard. Here’s the translated Ruby version:

list = Array.new

list.push(1)
list.push(2)
list.push(3)
list.push(4)
list.push(5)

list.each {|i| puts i.to_s }

and here’s the “real”, not-writing-Java-in-Ruby Ruby version:

list = []

(1..5).each {|i| list << i}

list.each {|i| puts i.to_s }

So if you ever wonder why I would rather not program in Java (and apparently I’m not alone), that’s your answer.

Speedy recovery

I watch one, and only one, car TV show whatsoever. It also happens to be the best such show anywhere: Top Gear. (I know, I know, it also has the humblest audience. I’m working on it.)

Accordingly, it is with considerable relief that I hear that Richard Hammond’s probably going to be just fine after the wild crash earlier this week. I join many others in wishing him a speedy recovery. It just wouldn’t be Top Gear without him.

House

House just started running here, and it’s one of the best TV series I have ever seen. It does for medical series what CSI did for law series. Hugh Laurie does a good job of hiding his accent and looking like John Glover.

If you, like me, think most medical series are dull (except for Scrubs, which is hyperactive), try watching House.

Older posts »