extconf.rb:4:in `require’: no such file to load — mkmf (LoadError)

I’ve hit this a dozen times and I always forget the solution. When you get this on Ubuntu,

sudo apt-get install ruby-dev
Posted in ruby | Tagged , , , , , , , | Leave a comment

Quickie: ssh Through Two Hosts in One Command

Say you want a shell on a box but you have to hop through another box to get it.

force@harold:~% ssh admin@maude
maude:~ force$ ssh root@whee
root@whee:~$ echo w00t
w00t

You can do that in one line using -t (pseudo-tty allocation).

force@harold:~% ssh -t admin@maude ssh root@whee
root@whee:~$ echo w00t
w00t

After playing with it, I can only get this to work for one hop. So yes, it’s of limited use. But I hop through another box every time I ssh at work, so I’m going to be getting a lot of mileage out of this little optimization. :)

Posted in quickies | Tagged , , , , , | Leave a comment

JavaScript Performance: Iterating over Arrays with Holes

I was sketching some code that basically took some data from a database and cached it in an array to be repeatedly reused. Conveniently, each row from the database had a unique ID, so I could use that as an array index. But is that a good idea? It just means that when I iterate over the array, I’ll need to check whether an element is defined before I try to use it. Is it better to code around this? Say, storing the id and other information in an object and simply pushing the objects onto the array? This would save needly iterating over undefined values later. Or is it better to use the id as an array index so that I can do quick and dirty iterations with checking? This sounds like a job for jsPerf! So, following are the details of my experiment. If you like, you can check it out in full and try the jsPerf yourself.

The Setup

Here are the simple and complex arrays, the select indexes for the entries that we want to look up later, and the pairs of IDs and names that comprise our data. At the end, there’s a get() function so that we can look up entries without a numerical index.

var simple = [], complex = [],
  selectIndexes = [1, 2, 10, 14, 300, 1000],
  pairs = [
    { "id" : 1,
      "name" : "Albert"
    },
    { "id" : 2,
      "name" : "Bailey"
    },
    { "id" : 4,
      "name" : "Charlotte"
    },
    { "id" : 5,
      "name" : "Darlene"
    },
    { "id" : 10,
      "name" : "Edna"
    },
    { "id" : 12,
      "name" : "Faron"
    },
    { "id" : 13,
      "name" : "Gary"
    },
    { "id" : 14,
      "name" : "Helen"
    },
    { "id" : 15,
      "name" : "Igor"
    },
    { "id" : 16,
      "name" : "Justin"
    },
    { "id" : 17,
      "name" : "Kyle"
    },
    { "id" : 300,
      "name" : "Lynette"
    },
    { "id" : 500,
      "name" : "Morgan"
    },
    { "id" : 1000,
      "name" : "Nora"
    }
  ],

  get = function (a, i) {
    var ret = null;
    $.each(a, function () {
      if (this.id === i.valueOf()) {
        ret = this;
      }
    });
    return ret;
  };

The Experiment

simple

A simple array with integer indexes and holes.

$.each(pairs, function () {
  simple[this.id] = this.name;
});

$.each(simple, function () {
  if (this && this !== window) { // this !== window is a workaround for IE
    this.charAt(0);
  }
});

$.each(selectIndexes, function () {
  simple[this].charAt(0);
});

complex

A complex array which holes objects with id and name attributes. Since we’re not using integer indices, we have to supply a get function (defined in Setup above).

$.each(pairs, function () {
  complex.push(this);
});

$.each(complex, function () {
  this.name.charAt(0);
});

$.each(selectIndexes, function () {
  get(complex, this).name.charAt(0);
});

The Results

Have a look at this graph:

Browser performance graph. Simple arrays with holes are the clear winner.

Simple arrays with holes are the clear winner.

Conclusion

The simple implementation just wipes the floor with complex one across the board. Not only is it easier to implement and read, but checking whether an element is undefined before you access it appears to be a trivially cheap operation. Arrays with holes are simple and safe to use.

Posted in javascript, Uncategorized | Tagged , , , , , , , | 3 Comments

Defining Functions Inline Is Just Fine

I put together this jsperf to see if there’s a difference between defining your function and then using it or defining it inline when looping with jQuery.each. You can try it yourself if you like, but here are the results:

I compared using a pre-defined function with using a function defined inline.

var body = $('body'); // cache this for use with both

//pre-defined
//
var doSomething = function () {
  body.append('<div class=' + this);
}
$.each([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], doSomething);

// inline
//
$.each([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], function () {
  body.append('<div class=' + this);
});

Here’s a graph of the results of different browsers performing the test:

Performance of different browsers running $.each with pre-defined and inline functions

Don't take this as an indication of the general performance of the browsers mentioned. Some were run on Linux, some on Mac, one in Wine on Linux, and some in Windows VMs on Linux.

The difference is negligible, so use whichever looks more readable to you. I prefer defining my functions inline, and will now do so with confidence.

Posted in javascript | Tagged , , , , | 1 Comment

Show Duplicate Filenames with Ruby

Here’s a quick one. I want to show just the names of files that have the same basename in a directory.

Code

require 'find'
require 'set'

all_files  = []
duplicates = {}
basenames  = Set.new

ARGV.each do |dir|
  Find.find dir do |file|
    basename = File.basename file
    all_files << file
    unless basenames.add? basename
      if duplicates[basename]
        duplicates[basename] << file
      else
        duplicates[basename] = [all_files.find{|f| File.basename(f) == basename}, file]
      end
    end
  end
end

puts duplicates.values.flatten.sort_by.with_index{|f,i| [File.basename(f), i]}

Example

Here’s some example terminal interaction.

force@zit:/tmp/demo% find .                                                                                                              .
./Pictures
./Pictures/February
./Pictures/February/family.jpg
./Pictures/February/fruit.jpg
./Pictures/index.html
./Pictures/January
./Pictures/January/car.jpg
./Pictures/January/family.jpg
./Pictures/March
./Pictures/March/dogs.jpg
./Pictures/March/cats.jpg
./Videos
./Videos/Aruba
./Videos/Aruba/scenic.mkv
./Videos/Aruba/water.mkv
./Videos/index.html
./Videos/Grand Canyon
./Videos/Grand Canyon/scenic.mkv
./Videos/Grand Canyon/water.avi
./Videos/Grand Canyon/walls.mkv

force@zit:/tmp/demo% ruby ~/show_duplicates.rb .                                                                                         ./Pictures/February/family.jpg
./Pictures/January/family.jpg
./Pictures/index.html
./Videos/index.html
./Videos/Aruba/scenic.mkv
./Videos/Grand Canyon/scenic.mkv
force@zit:/tmp/demo%
Posted in ruby | Tagged , , , | Leave a comment

Ruby instance_eval gotcha

I don’t know how much of a “gotcha” this is since it’s just a defined behavior and feature of the language, but I still found it a little counterintuitive when I encountered it, and I had to actually read the source of the Gem I was using to figure out what was going on. Specifically, I was doing this with Sinatra and Mail:

get '/' do
  @body = params[:body]
  Mail.new do
    to      'someone@example.com'
    from    'me@example.com'
    subject 'Gotcha!'
    body    @body
  end
end

That resulted in an

undefined method `length' for :Mail::Body

around that body @body line. Hrm? So I look at the source, and I find that after a bit of wrapping, eventually Mail’s message.rb is calling this.

instance_eval(&block)

And that’s how I learned about instance_eval. It takes a block, then executes that block in the context of the instance which calls it. So it effectively changes any references to self to be the instance where you write it. Therefore, my reference to @body was looking for @body in Message and not in my own app. This will probably be clearer with another example. Say we have these two classes:

class Foo

  def speak
    'arf!'
  end

  def act &block
    local = true
    external = 0
    instance_eval &block
  end
end

class Bar

  def speak
    'hello'
  end

  def dizzy?
    true
  end

  def do
    @name = 'dirt'
    age = 27
    local = false

    p speak    # "hello"
    p dizzy?   # true
    p @name    # "dirt"
    p age      # 27

    Foo.new.act do
      p speak    # "arf!" - because we are referring to Foo::speak
      p dizzy?   # Runtime error! Foo does not have a dizzy? method!
      p @name    # Runtime error! Foo does not have a @name variable!
      p external # Runtime error! This block doesn't have access to local variables in Foo::act!
      p age      # 27     - wait...that worked?
      p local    # false  - This local is used and not the one from Foo::act.
    end
  end

end

Bar.new.do

Gotcha again! Your local variables are still passed into the block!

So, instance_eval is why the rest of that block works with the calls to the to, from, and subject methods. Those methods aren’t defined in my context, and that was my first clue. Those methods are defined in Message‘s context, and instance_eval gives my block access to them, but that instance can now access my local variables (but not my instance variables), and my block cannot access the local variables of the context calling instance_eval.

Whew! I get it now, but it still sounds a bit complicated.

Posted in ruby | Tagged , , , , | Leave a comment

JavaScript Strings: instanceof vs. typeof

I’ve discovered some really interesting (read as “horrible”) behavior in Safari 5 and Internet Explorer 9. I was using this with great success in Chrome and Firefox.

if (typeof this === 'string') {
    doStuffWith(this);
}

Then I test in IE9, and it doesn’t work at all. Big surprise. But in Safari, it’s intermittent! So I start debugging, and I find that Internet Explorer is always returning false. But the weirdest thing is that Safari seems to be doing some kind of optimization in its JavaScript VM where it is true the first time, but false every time you hit reload!

My brain almost exploded.

So now I’ve settled on this:

if (this instanceof String || typeof this === 'string')
    doStuffWith(this.toString());
}

And now everything works great. Note that you can call “a string”.toString() and it just returns a copy of the string, i.e.

"a string".toString() === new String("a string").toString(); // true

So I’ll be using both from now on.

Posted in javascript | Tagged , , , , , , , , , , , , | Leave a comment

Run Sinatra App in a Subdirectory with Rack::Builder.map

Thank god I found Tanner Burson’s blog. I was about to explode. I recommend reading Tanner’s original post, but the short answer is that you want Rack to do some routing for you via Rack::Bundler.map. Here is a super simple example to get you running your Sinatra app in a subdirectory.

ronk.rb

require 'sinatra/base'

module Ronk

  class Foo < Sinatra::Base

    get '/' do
      'I am Foo'
    end
  end

  class Bar < Sinatra::Base

    get '/' do
      'I am Bar'
    end
  end

end

config.ru

require 'sinatra'
require File.join(File.dirname(__FILE__), 'ronk')

disable :run

map '/' do
  run Ronk::Foo
end

map '/bars' do
  run Ronk::Bar
end

nginx

Just for fun, let’s say you’re trying to reverse proxy through nginx.

(These are just the relevant parts. Google for a more thorough nginx or Apache configuration.)

proxy.conf

proxy_redirect          default;
proxy_set_header        Host                $host;
proxy_set_header        X-Real-IP           $remote_addr;
proxy_set_header        X-Forwarded-Host    $host;
proxy_set_header        X-Forwarded-Server  $host;
proxy_set_header        X-Forwarded-For     $proxy_add_x_forwarded_for;

nginx.conf

server {

  server_name  example.org;

  location / {
    proxy_pass http://localhost:9292/;
    include proxy.conf;
  }

  location /bars/ {
    proxy_pass http://localhost:9292/bars;
    include proxy.conf;
  }
}

Run the server

$ rackup # or unicorn, or thin, or whatever

Sample GETs

$ GET http://localhost:9292/
I am Foo
$ GET http://localhost:9292/bars/
I am Bar
Posted in Sinatra | Tagged , , , , , , , , , , , , , , , | Leave a comment

That’s Profiling!

I want to see if one string exists in another, case-insensitive. What’s faster?

  1. Making the strings both lower case then comparing them with String.match?
  2. Comparing the first string to a case-insensitive regular expression of the other?
  3. Making them both lower case, then using String.indexOf?

Let’s just try all three and quickly profile them.

/*jslint browser: true, indent: 2 */
/*global console */

(function () {

  'use strict';

  var start, stop, i, j, k, total, funcs,
    times = 100000,
    tests = 10;

  function getTime() {
    return (new Date()).getTime();
  }

  function lower() {
    return 'Potato'.match('POTATO'.toLowerCase()) !== null;
  }

  function regexp() {
    return 'Potato'.match(new RegExp('POTATO', 'i')) !== null;
  }

  function index() {
    return 'Potato'.toLowerCase().indexOf('POTATO') > -1;
  }

  funcs = [
    [lower, 'toLowerCase match: '],
    [regexp, 'case-insensitive Regular Expression: '],
    [index, 'indexOf: ']
  ];

  for (k = 0; k < 3; k += 1) {

    total = 0;

    for (i = 0; i < tests; i += 1) {

      start = getTime();
      for (j = 0; j < times; j += 1) {
        funcs[k][0].call();
      }
      stop = getTime();

      total += stop - start;
    }
    console.log(funcs[k][1] + total);
  }

}());

Output on Firefox:

toLowerCase match: 523
case-insensitive Regular Expression: 3859
indexOf: 482

Output on Chrome:

toLowerCase match: 783
case-insensitive Regular Expression: 9027
indexOf: 142

String.indexOf is the clear winner here. :)

Posted in javascript | Tagged , , | Leave a comment

Are You Still There?

A scenario: you want to update some content on your page periodically, but you don’t want to waste requests, resources, cycles, etc. on running your script while the user is obviously not interacting with the page. What do you do?

Here’s a jQuery extension I call “Still Alive” which handles this for you. It will run your functions periodically while the user is interacting with the page, but then it will go to sleep and stop executing them if the user goes away, and resume when the user comes back.

Implementation

/*!
* Still Alive v1.2
* http://github.com/sidewaysmilk/still-alive
*
* Copyright 2011, Justin Force
* Licensed under the BSD 3-Clause License
*/


/*jslint browser: true, indent: 2 */
/*global jQuery */

(function ($) {

  'use strict';

  // default values for optional arguments
  var DEFAULT_WAKEEVENTS = 'mousemove mousedown mouseup keydown keyup',
    DEFAULT_INTERVAL     = 60000,
    DEFAULT_IMMEDIATELY  = true;

  // sugar. Just get the current time in milliseconds
  function getTime() {
    return (new Date()).getTime();
  }

  $.stillAlive = function (callback, interval, immediately, wakeEvents) {

    var ptr,                // pointer to the interval so it can be cleared from outside
      args,                 // object containing optional arguments
      awake = true,         // awake status. Are we awake?
      lastSeen = getTime(); // the last time the user was seen (a wake event triggered)

    // For named arguments, copy the arguments object and assign its supported
    // properties.
    if (typeof interval === 'object') {
      args = interval;
      interval = args.interval;
      immediately = args.immediately;
      wakeEvents = args.wakeEvents;
    }

    // set default values for optional arguments
    if (interval === undefined) {
      interval = DEFAULT_INTERVAL;
    }
    if (immediately === undefined) {
      immediately = DEFAULT_IMMEDIATELY;
    }
    if (wakeEvents === undefined) {
      wakeEvents = DEFAULT_WAKEEVENTS;
    }

    // true if it's been (1.5 x interval) milliseconds
    function timeToSleep() {
      return (getTime() - lastSeen > (interval + (interval / 2)));
    }

    // set status to awake and update the lastSeen time, then execute the
    // callback.
    function wake() {
      if (!awake) {
        callback();
      }
      awake = true;
      lastSeen = getTime();
    }

    // set status to !awake (asleep)
    function sleep() {
      awake = false;
    }

    $(window).bind(wakeEvents, wake);

    // if enough time has passed without a wake event, sleep. If we happen to
    // be awake, execute the callback.
    ptr = setInterval(function () {
      if (timeToSleep()) {
        sleep();
      }
      if (awake) {
        callback();
      }
    }, interval);

    // if we're supposed to run immediately, execute the callback once
    if (immediately) {
      callback();
    }

    return ptr;
  };

}(jQuery));

Examples

Say I have a function called update defined elsewhere. I want update to be called every 60 seconds unless the user doesn’t seem to be around. I’d just

$.stillAlive(update);

How about every 15 seconds?

$.stillAlive(update, 15);

Let’s toggle a class of some elements every 90 seconds, but only if the user has clicked or typed something, and don’t run it immediately after being set.

$.stillAlive(function() {
  $('.togglies').toggleClass('toggly');
}, 90, false, 'click keyup');

The previous code could also be more explicitly called thusly:

$.stillAlive(function() {
  $('.togglies').toggleClass('toggly');
}, {
  interval: 90,
  immediately: false,
  wakeEvents: 'click keyup'
});

And for good measure, here’s a call using object literal notation, but only adjusting the interval:

$.stillAlive(update, { interval: 5 });

Note that the stillAlive method returns the intervalID, so you can cancel your stillAlive thusly:

var sa = $.stillAlive(update);
clearInterval(sa);

GitHub

The project, like most of my work, lives at GitHub. For complete documentation, forking, and keeping up with the latest version, check out Still Alive on GitHub.

Posted in javascript | Tagged , , , , , , , , , | Leave a comment