Coder for hire

Ah, the thrill of working for a Web startup: one day you think you might get an obscene raise; the next you discover that the company is out of business.

And yet, I’m addicted to them.

If you or someone you know needs a programmer for their Web startup (or any other job, really), I would be delighted to fill that position.

Unsure? Check out why you should not hire Mike Burns, my résumé, and the rest of this blog.

Edit: This is in and around Boston, MA.

An updated way to spider the Web with Ruby

I’ve released version 0.2.0 of Spider. Everything has changed:

  • Use RSpec to ensure that it mostly works.
  • Use WEBrick to create a small test server for additional testing.
  • Completely re-do the API to prepare for future expansion.
  • Add the ability to apply each URL to a series of custom allowed?-like matchers.
  • BSD license.

The new API is kinda cool. From the README:

Spider.start_at('http://mike-burns.com/') do |s|
  # Limit the pages to just this domain.
  s.add_url_check do |a_url|
    a_url =~ %r{^http://mike-burns.com.*}
  end

  # Handle 404s.
  s.on 404 do |a_url, err_code|
    puts "URL not found: #{a_url}"
  end

  # Handle 2xx.
  s.on :success do |a_url, code, headers, body|
    puts "body: #{body}"
  end

  # Handle everything.
  s.on :any do |a_url, resp|
    puts "URL returned anything: #{a_url} with this code #{resp.code}"
  end
end

I just uploaded it to Rubyforge, so give it a minute then gem update spider.

Uberman sleep minder

I’ve been working on the uberman sleep schedule for the past week-and-a-half. Here the crontab I use to help remember to follow the schedule:

DISPLAY=:0.0
0 2,6,10,14,18,22 * * * /usr/local/bin/dbus-launch /usr/local/bin/notify-send -u low -t 5000 -i stock_timer 'Sleep in 30 minutes' 'Your sleep is coming up in a half hour'
25 2,6,10,14,18,22 * * * /usr/local/bin/dbus-launch /usr/local/bin/notify-send -u normal -t 5000 -i stock_timer 'Sleep in 5 minutes' 'You should head to bed now'
30 2,6,10,14,18,22 * * * /usr/local/bin/dbus-launch /usr/local/bin/notify-send -u critical -t 5000 -i stock_timer 'Sleep now' 'You should not be awake to read this'

This uses notify-send to display a DBus alert for about a five seconds. Three such alerts exist: one 30 minutes before I sleep, one 5 before, and one when I should be asleep.

Adjust for your own schedule as you see fit.

Yahoo! Local vs. Yahoo! Local’s API

Yahoo! Local is pretty awesome; it can tell you all the stores named “Shaw’s” near the zip code 02215, for example. Yahoo! Local’s API, however, gives different results depending on its mood— sometimes the Shaw’s in 02215 is returned second; sometimes fifth; sometimes not at all.

Try this URL instead: http://api.maps.yahoo.com/ajax/locsrch

It takes these parameters:

  • appid
  • ll
  • t
  • q
  • r
  • n

appid is your application ID. If you use the Yahoo! APIs, you have one.

ll is the latitude and longitude, with a | (pipe) between them. Encoded, the | is %7C ; remember this if you are using Ruby’s URI.parse, for example.

t is 1; 1 means local search.

q is the query. It may only contain these characters, so delete the rest: [a-zA-Z0-9_.-]

r is the radius in miles.

n is the number of items to return. There’s probably an upper bound, somewhere around 20.

This produces JavaScript which can be evaled. In Ruby, you can do this instead (let javascript be the result of calling this URL):

require 'json'

if javascript =~ /\\\\((.*),.*,.*\\\\);/
  json = $1
  results = JSON.parse(json)
  items = results['ITEMS']
end

Now items is an array of hashes with at least these keys as strings: CITY, LATITUDE, LONGITUDE, STATE, ADDRESS, TITLE. No ZIP, though.

`call/cc’ and Self-modifying Code, in Ruby

If you read Matthias Felleisen’s blog post today about using call/cc to write a generator, and you’re anything like me, you must have thought “Ruby has call/cc; how would I do this in Ruby?”

This is my first pass. It works, but it could very well be broken, and I know it can have a better interface.

class Array
  def generate_one_element_at_a_time
    control_state = Proc.new do |ret|
      each do |an_element_from_a_list|
        callcc do |resume_here|
          control_state = Proc.new do |r|
            resume_here.call(r)
          end
          ret.call(an_element_from_a_list)
        end
      end
      ret.call(:you_fell_off_the_end_of_the_list)
    end
    Proc.new { callcc {|k| control_state.call(k)} }
  end
end

It is used like this:

irb(main):001:0> a = %w(a b c).generate_one_element_at_a_time
=> #
irb(main):002:0> a.call
=> "a"
irb(main):003:0> a.call
=> "b"
irb(main):004:0> a.call
=> "c"
irb(main):005:0> a.call
=> :you_fell_off_the_end_of_the_list
irb(main):006:0> a.call
=> :you_fell_off_the_end_of_the_list

Function-scope state in JavaScript

I needed to write some JavaScript that toggles its output. In Scheme it looks like this:

(define img-toggler
  (let ((down? #t))
    (lambda ()
      (if down?
          (begin (set! down? #f)
                 "right.gif")
          (begin (set! down? #t)
                 "down.gif")))))

JavaScript doesn’t have `let’ expressions, so I wrote an approximation of one:

var let = function(state) {
  var cb = new (function(state, args){
    this.state = state;
    this.call = function() {
      var ret = null;
      for(var i = 1; i < args.length; i++)
        ret = args[i](this.state);
      return ret;
    }
  })(state, arguments);
  return function() { return cb.call() };
};

And used it like this:

var imgToggle = let({on: true},
    function(state) {
      state.on = !state.on;
      if (!state.on) return 'right.gif';
      else return 'down.gif';
    });

My Rails Development Environment, version 2: Scribes, Nautilus, and gnome-terminal

On a whim I tried Scribes for a day. It’s not terrible: autosaving, keyword completion, templates, syntax highlighting, and a sleek look. There’s a lot I miss from vim, but after two weeks of Scribes I’m mostly adjusted. (I really, really, really look forward to the autosave misfiring fix.)

At first I thought I couldn’t do a Rails project with Scribes because, since it’s not command-line, I couldn’t use screen. Discovery: I can replace screen with a mix of Nautilus and gnome-terminal.

Nautilus offers a beautiful tree view of the whole project. Any file can be rather easily selected and opened, via mouse or keyboard. The non-file stuff, such as databases, local servers, and Rails scripts, can be run from a gnome-terminal with three tabs (./script/server, psql, and a catch-all tab).

Nautilus, Scribes and gnome-terminal

The limitations are: I have a lot of models and, as such, they’re tedious to find. Also, I still have to use the terminal. I’ll fix these limitations in the third post.

My Rails Development Environment, version 1: vim and screen

Lately I’ve changed the way I manage the code on my Rails projects; this is the overview on how I did it for the past two years.

I’ve used vim since my first programming job eight years ago. I’ve use it for email, papers, programming, notes—anything. I instinctively hit escape when I finish typing; my blog posts are filled with j, :wq, and a; my .vimrc represents five jobs and the seven programming languages used in those.

So, of course, my text editor for Rails was vim.

The next issue was that I had to bounce between editing files in five directories; running ./script/console, svn ci, ./script/generate, and ./script/server; looking at the database; and managing the images, JavaScript, and CSS. Vim was good, but I needed fewer keybindings. So, I finally learned screen.

After a bunch of customization, this is what I have:

~/.zshrc:

alias scrails='screen -c ~/.screenrc-rails'

~/.screenrc-rails:

hardstatus alwayslastline %w
screen -t controllers 0 sh -c 'cd app/controllers; zsh'
screen -t models      1 sh -c 'cd app/models; zsh'
screen -t views       2 sh -c 'cd app/views; zsh'
screen -t project     3
screen -t db          4
screen -t public      5 sh -c 'cd public; zsh'
screen -t schemae     6 sh -c 'cd db/migrate; zsh'
screen -t server      7

~/.vim/plugin/rails.vim.

Integer#to_word

The other day some friends and I came across a job posting for ITA Software on the T. It read:

If the integers from 1 to 999,999,999 are written as a word, and concatenated, what is the 51 billionth letter?

The eight of us spent the duration of the ride discussing this question and its solutions. This inspired me to write Integer#to_word for Ruby, as part of one possible way to solve it.

From the tutorial:

> 999999.to_word
=> "nine hundred ninety nine thousand nine hundred ninety nine"

JavaScript’s String Identity Method is Not Trivial

This bit of JavaScript confused me. This is tested on Gecko’s JavaScript engine only.

String.prototype.i  = function() { return this; }
String.prototype.i2 = function() { return this+""; }
typeof("a string".i())  == "string" // => false
typeof("a string".i2() == "string" // => true