Archive for August, 2008

Installing memcached on OS X 10.5.4 Leopard

Posted on August 19th, 2008 in memcached | 6 Comments »

I used the guide here to install Memcached on my Macbook Pro, but seeing how it was written over 3 years old I decided to write an updated version for installing on OSX Leopard. Here goes:

Beforehand: You need to install Xcode 3.0 Developer Tools. If you don’t have it installed it’s on the Leopard Install CD that came with your Mac. You also have to install the X11 windowing environment on the same disc.

  1. If you don’t have it already, download Macports:

    Here’s the Leopard universal binary direct link: (416kb)

  2. Macports will install itself to /opt/local/bin but on my machine I didn’t have a PATH variable set there so if you type “port” in your terminal and don’t get anything either, continue following this step, else just continue on to the next one.

    Open up terminal and type:

    nano .bash_profile (return key)

    Find the first unused line and type:


    To save your changes press ctrl-o (return key) and ctrl-x to quit.
    Refresh your profile by typing:

    . .bash_profile
  3. To test that your Macports is working (and to update to the newest version):
    sudo port -v selfupdate
  4. Install libevent. It’s a dependency of memcached:
    sudo port install libevent
  5. Install memcached:
    sudo port install memcached
  6. Set an environment variable to stop libevent using kqueue (should force it to use select, which is slower, but actually works). I have no idea what this does, just following the original guide, but it’s related to making memcached faster :

    Open up terminal and type:

    nano .bash_profile (return key)

    Find the first unused line and type:


    To save your changes press ctrl-o (return key) and ctrl-x to quit.
    Refresh your profile by typing:

    . .bash_profile
  7. Run memcached (this gives it 24MB of memory max, and puts it on port 11211 – note that specifying a hostname will not work on OS X):
    memcached -d -m 24 -p 11211
  8. Telnet to your memcache to make sure it’s working:
    telnet localhost 11211

    If you see “Connected to localhost”, you’re connected.

    Type quit to close the connection and try reconnecting again to make sure you did step 6 properly.

    You’re done!

To stop memcached, type this in your console:

killall memcached

To start memcached in verbose mode (so it outputs what it’s doing) add -vv to the startup script.

Feel free to create a bash script to startup memcached with your right settings, mine looks like this:

#!/usr/bin/env bash

memcached -d -m 24 -p 11211
echo "memcached started..."

Related links:
Installing memcached on OS X 10.4 Tiger
Installing Macports
Memcached homepage

Further reading:
Setup a Memcached-Enabled MAMP Sandbox Environment
Install ruby memcached on MacOSX

How to add Ruby (.rhtml) support to your existing MAMP setup with eRuby

Posted on August 18th, 2008 in Apache, ruby | No Comments »

Adding Ruby HTML support to your existing MAMP setup is great for experimenting and for building quick single serving apps where a whole Rails setup would be overkill. This is how I added Ruby RHTML page support to my current MAMP setup:

Download and compile eRuby

  1. Download and unzip
  2. Go to the unzipped directory in terminal
  3. type ./configure.rb
  4. type make
  5. type make install
  6. Copy the generated eruby file to your local cgi-bin

Modify your httpd.conf settings

  1. add this line to add the rhtml support:
    AddType application/x-httpd-eruby .rhtml
    Action application/x-httpd-eruby /cgi-bin/eruby
  2. And modify this line so your server will look for index.rhtml pages in directories:
    DirectoryIndex index.html index.shtml index.rhtml

Running straight .rb files outside your cgi-bin folder (optional)

You can run any scripts from your cgi-bin if you properly add the #!(she bang) location to your ruby intepreter at the top of your scripts, but you can also make these scrips run outside of your cgi-bin by just adding this line to your httpd.config, just add the .rb extension to the filename:

AddHandler cgi-script .rb

I was doing this for awhile so I could execute scripts through the web but the RHTML method is better. Still, there could be applicable reasons for a person to want to do this. Just keep in mind, that when accessing Ruby scripts directly from a webpage you must at least specify a content-type (puts: “content-type: text/plain\n”) in your header or use the convenient cgi class from the Ruby standard library that does that (and a whole lot more) for you.

Improving Performance (optional)

The above is a really quick way to add Ruby support to your local webserver however, there’s one downside – it has to start up the Ruby interpreter everytime you hit an rhtml file with your browser. If you want something a little more dedicated look into the mod_ruby apache module which embeds a Ruby interpreter into Apache’s (inside your web server’s memory), to let Ruby CGI scripts execute natively, so scripts start up and run far faster. This is the equivalent as mod_php for PHP, that’s built into your MAMP setup.

Additionally, as a speed boost, you could use fast cgi to keep the eRuby interpreter instantiated, if for some reason you can’t use mod_ruby.

Helpful links:

JavaScript argument unpacking: converting an array into a list of arguments

Posted on August 17th, 2008 in JavaScript | 6 Comments »

Ruby has a cool operator by the name of splat(*) that lets you unpack an array as arguments on the fly. See this code below for an example:

# Define the item array
item1 = ['Jack', '39', 'Panda']
# Define a regular function that takes three arguments
def hi(name, age, type)
  puts 'Hi ' + name + '! You are ' + age + ' and a ' + type + '!'
# Call the function using item1 and the splat operator
# Outputs: Hi Jack! You are 35 and a Panda!

While JavaScript does not have such a convenient operator, experimenting around, I’ve found a good enough solution:

var item1 = ['Jack', '39', 'Panda'];
function hi(name, age, type){
     console.log('Hi ' + name + '! You are ' + age + ' and a ' + type + '!');
hi.apply(this, item1);
// Outputs to your Firebug console: Hi Jack! You are 39 and a Panda!

The apply method is built into all functions in JavaScript and allows you to call a method of an object in the context of a different object (Moz dev page here). Think about it as the equivalent of temporarily moving that method off the current object (the window object if it’s a global function) and attaching it to another object before calling it.

The primary reason to use this method is for changing the this keyword reference inside of the called function, but it has the added benefit of optionally requiring an array of arguments that get passed to the called function. We basically keep the this keyword as the window object (could have specified window instead of this) and use it to unpack our array for us.

This solution is also useful in any situation where you have a function that takes an unlimited number of arguments and you want to to eventually send those arguments to another function that also accepts any number of arguments. This situation is exactly what was puzzling me as I was working on creating a “safe” version of Firebug’s console.log that would work normally in Firefox but alert each argument separately in Internet Explorer.

Lastly, this is usefully for succinct array merging. Instead of looping through one array and pushing each item onto another you can do:

bob = [1,2,3,4];
sue = ['a','b'];
// returns [1,2,3,4,a,b]

Pwning (JailBreaking) your iPhone 3g with 2.0.1 firmware

Posted on August 11th, 2008 in iPhone | No Comments »

My brother Mark, his girlfriend Jen, and their iPhones

Excuse another iPhone posts, I have some juicy programmerly entries coming up I just have a few friends that are interested in this and want to save them some time.

The 2.0.1 firmware which came out just a few days ago was just the thing I was waiting for – it fixed the craziness and slowness that was going on with (all?) my iphone (hour long backups anyone?) as well as the slow texting and “apple screen of death”. It came, in fact, just when I decided to jailbreak my iPhone for the second time.

The first time I did it was back before 2.0 firmware and it was the high life. I had SSH, VNC clients, silly games, and an Apache web server running from my phone. I gave all that up for the light saber app and international keyboard support. But now I have the best of both worlds again, and the five hours it cost me can be yours in less than one following my quick guide.

At first jailbreaking your iPhone was really hard. You had to do all kinds of things in terminal and cross reference about three or four tutorials that all weren’t quite right. Then it got really easy – just go to a web site using your phone’s browser and BAM, your iPhone got to pass go and collect two hundred dollars. Now it’s somewhere in the middle. Freedom is in the form of a downloadable app called Pwnage (Mac only but there should be a Windows equivalent by now). Basically, jailbreaking your phone under the 2.0 firmware involves rewriting the latest firmware update and making a custom “altered” one for you to restore with – don’t worry it’s a lot easier than it sounds and as far as I can tell, it’s impossible to brick your phone, it would have happened to me from all the things I’ve tried. So here’s what to do:

  1. Backup your iPhone and restore it to factory condition (you can restore your backed-up files later.)
  2. Visit this link and download Pwnage to your desktop (don’t run it or unzip it):
  3. IMPORTANT: Do not use some app to unzip the file. It creates weird permission issues and it looks like 80% who tried this including myself wasted a lot of time reformatting their phones several times. Instead, open up Terminal, change directories to your desktop, and unzip the file with this command: tar -jxvf PwnageTool_2.0.2.tbz
  4. For some reason, iTunes keeps backups of all your firmware updates – this seems incredibly wasteful to me since they’re about 250mb a piece. Inside of /Users/USERNAME/Library/iTunes/Mobile Backups is where they’re located.

    Go ahead and move the 2.0.1 update (iPhone1,2_2.0.1_5B108_Restore.ipsw) to your desktop.

    (If you’re having trouble finding this folder just type open /Users/USERNAME/Library/iTunes/Mobile\ Backups, where USERNAME is the current logged in user. If you don’t know who the current logged in user is type whoami in Terminal.

  5. Ok, now run Pwnage in Expert mode (simple mode is awesome but it only auto-detects and tries to use the 2.0 firmware, not 2.01.) Browse for your latest software update file (IPSW) on your desktop. Here you have a bunch of options. Just do this:

    Click General, then the next button, and unclick Activate the phone. Hit the Back button.

    OPTIONAL: Click custom logos, next, and uncheck them if you don’t want them. Hit the back button.

    Click the Build button and the next button. In about 5-10 minutes you should have your new IPSW file. Here it should ask you to do some magically things with your iphone, including putting it into recovery mode. It’s SLIGHTLY tricky but you just have to get the timing right to get into recovery mode. Last time I remember, it’s 10 seconds on home and sleep buttons followed by 5 seconds on home button. Just follow the pictures (don’t look at the words, they throw you off.)

  6. Switch back to iTunes. It’ll say it detects your phone in revovery mode. Option click the restore button – it will allow you to pick a file. Pick the custom one from your desktop. In a few minutes you’re done!

    You should see 2 new icons on your springboard. The old familiar Installer, where you can install apps with. And the new installer called Cyndia. Try to open up Cyndia – if it crashes, that means there were some permission issues when you initially unzipped pwnage. Try again.

  7. Optionally, restore from your backup in step 1. Now install apps to your hearts content!
  8. Do note that right now a lot of 1.0 jailbreak apps haven’t been ported to the new 2.0 framework yet. Still there is a lot of cool stuff including having an interactive terminal, the ability to SSH/SFTP into your phone and SSH out to other machines, Ruby, Perl, Python, and Java support, Lighttpd web sever, an NES emulator, MxTube (let’s you download full res Youtube vids to your phone), lots of abilities to customize themes, logos, ringtones on your phone, SVN, CVS, IRC clients, Lynx, and the list goes on.

    If you have any trouble with the above and you have my phone number feel free to call me, else just leave a comment.