New Project — Daily Coding

I heard today about a guy who committed to commit some finished piece of his own code to a project every day for a year.

I thought it sounded good. So I decided to start with some project euler.

Hopefully, the various rails projects I have in mind will eventually be part of this, but I wanted to start somewhere small.

Ruby 1.9.3 on Mac OS X Lion with XCode 4.3

I wanted to install rails 3.2.4 on Mac OS 10.7. The system ruby is 1.8, but I wanted to run against 1.9.3, and it was tricky getting everything working with XCode 4.3.

I wanted to use RVM, but right now RVM spits out an error telling you that 1.9.3 is kind of covered with XCode 4.3, that prior versions of ruby aren’t supported with 4.3, and that you might want to install OSX-GCC-Installer and remove XCode. If you want to go down that path, the OSX-GCC dude seems like a nice bloke:

I didn’t! As such, I decided to see if homebrew would install ruby 1.9.3 (it does) and how hard it would be to switch between its version of ruby and the system ruby (it’s trivial).

  • Remove rvm if you’ve installed it:
    rm -Rf ~/.rvm
  • Install XCode 4.3 from the app store.
    Once installed, open XCode -> Preferences -> Downloads -> Command Line Tools. Download ’em.
  • Install HomeBrew: Do your best with their post-install instructions. I especially found it useful to uninstall macports
  • Install ruby 1.9.3: brew install ruby
  • Open .profile (or .bashrc or whatever bash config file you’re using) and add
    #homebrew suggests putting usr/local/bin before /usr/bin
    export PATH=/usr/local/bin:$HOME/.gems/bin:$PATH
    alias unbrew="export PATH=${PREPATH}"

    Now, if you want to use the system ruby you run unbrew from the command line

Sockets, Arbitrary Ruby and C

I’ve had this small project in mind to write a server which would execute arbitrary ruby. I didn’t want to have a concept of state, so assignment and memory management weren’t a big deal.  It should:

1. Accept a class method, like puts “Hello, world!”
2. Output on the server “Hello, world!”
3. Output on the client “puts “Hello, world!””

I knew GServer existed in the stdlib, but didn’t know how to work it. I saw a neat trick on the peepcode  screencast with Aaron Patterson: download the source tree and ask _that_ your questions, don’t bother with the docs.

I downloaded ruby’s source tree, kind of nervous, and found the GServer implementation using sublime text’s fuzzy search function. The comments said to just inherit from the class and implement the serve method, and all would rock.

I was surprised by how much cleared reading the source and comments were than reading the rdoc that’s generated from those same source and comments. I was reading through the code really happily, learning about GServer, and managed to get the functionality I was looking for in about 20 minutes. If you want to download the code, it’s available on github:

One of the neat things reading the code was that I actually read the implementation of each method, and I noticed something in the start method that got me thinking. There’s a class to create a new TCPServer:

 @tcpServer =,@port) — from GServer.rb

The requires for the class are socket and thread, so I assumed this came from socket. I read socket.rb, though, and I honestly didn’t see a TCPServer definition, so I checked its requires: is a compiled library. I did a quick search for socket.c and found documentation in there that references, but which doesn’t seem to have any implementation that would recognise the name “”.

Socket.c includes rubyserver.h. Reading that, I saw these two lines:

extern VALUE rb_cTCPServer;

void rsock_init_tcpserver(void);

So we’re mentioning a tcpserver in an included library, finally!

From here, I didn’t know where to go.I did a search of the source tree for tcpserver and found tcpserver.c. It also includes rubyserver.h, so presumably somewhere in that include is where the relationship between socket and tcpserver is created, but I can’t see where.

I do see where the class is made, though:

  * Document-class: TCPServer < TCPSocket
  * TCPServer represents a TCP/IP server socket.
  * A simple TCP server may look like:
  * require 'socket'
  * server = 2000 # Server bind to port 2000
  * loop do
  * client = server.accept # Wait for a client to connect
  * client.puts "Hello !"
  * client.puts "Time is #{}"
  * client.close
  * end
  * A more usable server (serving multiple clients):
  * require 'socket'
  * server = 2000
  * loop do
  * Thread.start(server.accept) do |client|
  * client.puts "Hello !"
  * client.puts "Time is #{}"
  * client.close
  * end
  * end
  rb_cTCPServer = rb_define_class("TCPServer", rb_cTCPSocket);
  rb_define_method(rb_cTCPServer, "accept", tcp_accept, 0);
  rb_define_method(rb_cTCPServer, "accept_nonblock", tcp_accept_nonblock, 0);
  rb_define_method(rb_cTCPServer, "sysaccept", tcp_sysaccept, 0);
  rb_define_method(rb_cTCPServer, "initialize", tcp_svr_init, -1);
  rb_define_method(rb_cTCPServer, "listen", rsock_sock_listen, 1); /* in socket.c */

So, as long as I can get from tcpserver.c through to socket.c, I think I’ll be okay.
Funny how you sometimes wander down paths.

Ellen Allien — Sehnsucht

Such a brilliant song.

The world’s moving by slowly, quietly. I’ve been spending my spare time mostly reading:

introducing html5, part of a series which, the publisher assures, is written by “voices that matter”. Quite what that means, I don’t know, but it’s a surprisingly fun book to wander through.
Beginning Visual C++ 2010. Visual Studio is a big mammoth of a tool, C++ is a big mammoth of a language. Together at last, I guess: seems like every page has a dozhundred things to remember, and I’m not even out of the chapters that are mostly concerned with the grammar and structures of C yet. Bloody ‘ell.
Javascript and Ajax, a part of the Visual Quickstart series of books. Surprisingly accessible, even downright funny at times. What’s with teh current trend of writing programming books that tickle you pink?

Still, my hope is that one day I’ll be some kind of lean, mean fighting machine on the web and desktop. Just give me ten years, I’ll get there. In the meantime: longing.

Windows 7 app

I’ve been thinking recently about a nice app for Windows 7 phones, targeted at the climbing market. It would be a way to take a photograph of a rock, and clearly mark a route up to climb.

The problem with a lot of climbing books is twofold: first, their pictures are relatively low quality with no route marking up a rock face indicating how to ascend (only a verbal description that’s often vague), and second there’s no real way to tell how to find the rock face that the book’s author is thinking of.

A windows 7 application could really solve both of these things: high quality photographs with the killer onboard cameras, with some basic editing skills like contrast adjustment, and the ability to draw a route dynamically onto a rock face using touch, we have the first problem solved. The second is solved by using the geolocation features inside the images that modern cameras save. Makes everything easy to find, and when you get there there’s a nice picture available (assuming someone’s already posted one).

I was thinking about tying this in with some online services, too: azure for backend storage, ability to post to a mountainproject page. This could be pretty slick if I get it right.

I’m thinking that this, tied with a powerful desktop application to navigate through and organise notes and whatnot for the phone, would be a pretty powerful service for climbers all over. That, plus it’d be a natural target for any upcoming tablet devices….

Trying to Improve

I’ve been writing a lot of perl recently, and had a few solid insights into what it means to program better than a beginner. Not like a professional, but not like a beginner.

Writing to the interface has been huge to me. Writing to the interface, not changing the interface.


Slowly working out how to keep my functions free of side effects is another. Incredibly hard to do, it’s taken me years to grok what it means for a function to have a side effect and to discipline myself to write side effect free code. I’m not great at it, but I’m better than okay.

A really interesting way to learn about side effect free code is to start writing ruby. The ruby community really encourages tons of small methods on an object, and that starts training your intuition to understand how to make one method do only one thing, and chain things together.


I have so much improvement to do, though. I don’t feel like I really understand perl or ruby or C or Objective C at all. C, I’m getting closer with: understanding how pointers and the address of operator work was crucial, as was getting a mental image of what a type is in C (think of it as a way of indicating how much memory something needs).


On another topic, Uncle Bob’s been giving some really interesting talks recently about what the new programming paradigm might include, what direction programming should be taking. His recent interview on the pragmatic programmer’s podcast is well worth checking out.