Archive for the ‘Ruby’ Category

h1

Speed tests in Ruby

April 11, 2008

Maybe this is already somewhere, but I did not find it in the

Test::Unit::TestCase

documentation where I expected it. I want to make sure that a certain block of code would execute within a given time period. Granted, it’s a bit obvious, but in case you searched first like I did and found this:

class Test::Unit::TestCase
def assert_faster_than(delta)
    start = Time.now.to_f
    yield
    finish = Time.now.to_f
    assert_in_delta start, finish, delta, "Expected to take less than #{delta} seconds but took #{finish-start}."
  end
end

# Then just use it like this....
assert_faster_than(3) { something_that_could_take_a_while }
h1

Installing Rails without root

December 9, 2007

Some people are concerned with keeping their /usr hierarchy clean. Some people have no root access to the system that they are on. Some people don’t like using /usr/local. These people still want to install and run Ruby and Rails, but it will be best for them if it is done in a way that will keep gem working as it should.

I like to tell these people, “Oh it should be easy,” but I had to really make sure. There is one little trick, which is to make sure you really are using the right ruby when you install rubygems and then the right gem when you install Rails (and forever after).

As much for my own benefit as for anyone else’s, I put together a script so that I don’t forget. This should work on any UNIX that has the build dependencies for Ruby. That includes Linux of flavors Redhat, Ubuntu, Debian, and Slackware for all you Google fans out there. (The guy whose problems prompted me to put this together was using Ubuntu.) I like Debian but have found that Rails changes way too fast for Debian packages to be a viable way to install it. Gem is much better and standard in the Rails community, so you should do a local install of Ruby from source and then install rubygems using that version.

Without further ado, here’s the script that will save you time. You will need to edit it some unless you want /opt/rails. Personally I use /usr/local. It will also leave a build subdirectory in whatever location you use. It is your option to remove it after the fact. I would do so myself.

#!/bin/bash

INSTALL_ROOT=/opt/rails
cd $INSTALL_ROOT
mkdir build

cd $INSTALL_ROOT/build
wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6-p111.tar.gz
tar zxvf ruby-1.8.6-p111.tar.gz
cd ruby-1.8.6-p111
./configure --prefix=$INSTALL_ROOT
make
make install

cd $INSTALL_ROOT/build
wget http://rubyforge.org/frs/download.php/28174/rubygems-0.9.5.tgz
tar zxvf rubygems-0.9.5.tgz

# This is crucial. rubygems setup really cares where ruby ran from, and if
# you have a system-wide ruby then it might get confused.
export PATH=$INSTALL_ROOT/bin:$PATH
# Of course you will also want to makde sure you include this wherever you
# normally set your PATH.

cd $INSTALL_ROOT/build/rubygems-0.9.5
ruby setup.rb
cd $INSTALL_ROOT

which ruby
which gem
gem install rake
gem install rails --source=http://gems.rubyonrails.org
gem environment
which rails
rails --version