Saturday, January 23, 2010

Prototype analog to jQuery's $(document).ready

I have a lot of experience with jQuery, but less with Prototype. Recently, I needed to add some event handlers to some elements in a Ruby on Rails app I'm building. I searched for how to do the equivalent of jQuery's $(document).ready() function in Prototype so that I could add the handlers after the document loaded, but most of the guides I found were out of date (I'm running Prototype 1.6.0.3, and I don't know which version these guides were for, but they all made my Javascript console angry).

Eventually, I was able to piece it together after digging through the depths of the Prototype API documentation. It's actually very simple:


document.observe('dom:loaded', function(){
// do yo thang...
});


Wrap whatever you're doing with that, and it won't be run until the document is loaded.

Wednesday, January 13, 2010

Installing Ruby on Rails 2.3+ plugins from github

I've been banging my head against this wall for quite awhile now, and I just finally figured out the answer. Like I've done in other posts, I'll just post what worked for me, and hopefully it'll help other people.

I'm running Ruby 1.9 and Ruby on Rails 2.3.3 on Snow Leopard. I've been trying to install plugins (specifically, Authlogic and Carmen) for a couple days now using the following two commands (as taken from the main github pages):

script/plugin install git://github.com/binarylogic/authlogic.git
script/plugin install git://github.com/jim/carmen.git


In return, I received the following errors:
Plugin not found: ["git://github.com/binarylogic/authlogic.git"]
Plugin not found: ["git://github.com/jim/carmen.git"]


After a lot of poking around, it turns out you need to make two changes in order for this to work on Rails 2.3 or higher: change the git:// at the beginning of each URL to http://, and add a trailing slash to the end of each URL. So instead, run these:

script/plugin install http://github.com/binarylogic/authlogic.git/
script/plugin install http://github.com/jim/carmen.git/


They both worked perfectly for me, so hopefully they'll work for you. If not, leave a comment and I'll try to help.

Friday, January 1, 2010

Installing Erlang on Snow Leopard

Here's another in my series of "Installing X on Snow Leopard". These aren't official, well-tested guides; they're just documentations of my attempts to compile and install various things on my personal computer. My last one (Installing MySQL on Snow Leopard) is my most popular post to date (aside from a couple that have been on Reddit). Erlang is less popular than MySQL, but hopefully this will still help a few people.

Downloading and unpacking


Go to http://erlang.org/download.html and download the Source for the newest version (when I was writing this, that was R13B03. After downloading, extract it to somewhere that's convenient to get to with the Terminal.

Configure


Open the Terminal and cd into the directory you extracted Erlang to (mine was /Users/jake/src/otp_src_R13B03 . Then run the following command:

./configure \
--prefix=/usr/local/ \
--enable-smp-support \
--enable-threads \
--enable-darwin-64bit


Note: You will probably get three errors. Read about them in the Configuration Errors section coming up.

The first three configure options are the defaults according to the README. However, I've had experiences where supposed defaults aren't really the defaults when compiled on OS X, so I don't like to take chances. --enable-darwin-64bit enables experimental support for the 64bit x86 Darwin binaries. This may not be necessary, but in general, 64-bit stuff has fewer problems on Snow Leopard, so I figured this was a good idea.

Configuration Errors



I got the following configuration errors:

jinterface    : No Java compiler found
wx : Can not combine 64bits erlang with wxWidgets on
MacOSX, wx will not be useable
documentation : fop is missing. The documentation can not be built.


These aren't a problem. If you get any errors besides these, you're in trouble. Leave a comment, and I'll see if I can help.

Making and installing



These two commands shouldn't give you any trouble:

make

And then, after make is done:

sudo make install

If you get any errors at either of these stages, leave a comment and I'll try to help.

Making sure it works



Note: This canonical test is gratefully borrowed from erlang.org.

Put the following into a text file:

-module(test).
-export([fac/1]).

fac(0) -> 1;
fac(N) -> N * fac(N-1).


Save it as test.erl in a directory that's easy to get to with the Terminal. Then, from the Terminal, cd into that directory and type erl (which, if everything worked right, should start the Erlang command-line interpreter). From the interpreter, run the following commands:

1> c(test).
{ok,test}
2> test:fac(20).
2432902008176640000
3> test:fac(40).
815915283247897734345611269596115894272000000000


Note: Lines starting with N> (where N is a number) are lines you should type (but just type the stuff coming after N>). The other lines represent output.

c(test). compiles test.erl (assuming it's in the directory you cd'ed into). test:fac(20). and test:fac(40). runs your factorial function.

So, that's what worked for me. If anyone has any problems along the way, leave a comment and I'll try to help.