Transform Matching Text with Gsub in Ruby and Regular Expression

Here is a gist that demonstrates how easy it is to transform text using #gsub and a block with Ruby.

For more helpful string extensions in Ruby check out our Ruby Gem on GitHub https://github.com/AgilionApps/rails_extensions

Color Output with Test:Unit, AutoTest and Ruby 1.9

If you are testing using Test:Unit (rather than RSpec) and you’re using Ruby 1.9.* colorized output of your tests using Autotest will not be immediately available. Since, 1.9 comes with mini test the test/unit/ui/console/testrunner.rb script is not loaded and not available and will break your tests.

The solution is to require Test:Unit version 2.0.0 in your Gemfile, require the testrunner.rb script in test/test_helper.rb and reopen and implement the guess_color_availability method.

Then you can just run the autotest command (or bundle exec autotest) from your project directory. When you save a file your tests will be run for the file that has been changed and the results will be fully colorized!

Email Regex

Regular Expression that Matches Email Addresses:

/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/

How to Upgrade RVM on Mac OS X

I had an old version of rvm installed and wanted to upgrade. So old in fact that the resource for upgrading no longer existed.

rvm update

just returned a 301, redirect.

Luckily, the following worked

# checks out from repo
rvm update --head
# will reload rvm environment
rvm reload
# finally, the upgrade command works!
rvm get latest

Ruby Reload! Method in Non Rails IRB Sessions

I love the Rails reload! function when in the console. I need it in Irb. To get it back this is what I did. If you don’t already have an .irbrc file in your home directory, just create it.

vim ~/.irbrc
or textmate if you prefer
mate ~/.irbrc

Add this little snippet to it…

unless defined?(reload!)
  $files = []
  def load!(file)
    $files << file
    load file
  end
  def reload!
    $files.each { |f| load f }
  end
end

Usage : To load files in irb just use the method we defined in the .irbrc file “load!” notice the bang “!”. I don’t want to overwrite the actual load method. This load! method will just put the file in an array before loading it, so when running reload! it will iterate over this collection and load them again with whatever changes have since taken place. The unless conditional is so that you don’t overwrite the reload! method if you’re actually in the rails console.

Ruby Rand Range

I assumed that rand would take a range as an argument. Something like rand(10..20), generating a random number between 10 and 20. Seems like you’d do this fairly often when working with random numbers and therefore, included. However, it doesn’t work. But the solution is almost as easy.

10 + rand(11) #=> produces a random number between 10 and 20

Since rand starts at 0 (like array indexes), we need to add an extra 1 to get what we’re expecting.

Installing Ruby on Rails 3, MySQL, Git, Ruby Enterprise Edition, Passenger (Mod_Rails) on Ubuntu with Rackspace Cloud.

Short and sweet. Here all the commands I run in this order to set up a brand new box. It usually takes about 10 – 15 minutes on a 256 MB RAM instance. Compiling Ruby Enterprise Edition, which is super easy, will take the most amount of time. It will seem to have gotten stuck. It hasn’t. It just takes a little while.

# Update, upgrade and install all necessary packages for Ruby on Rails server if you've got a fresh Ubuntu slice
apt-get update
apt-get upgrade

apt-get install build-essential patch libssl-dev libreadline5-dev

apt-get install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8 libopenssl-ruby imagemagick librmagick-ruby1.8 librmagick-ruby-doc libfreetype6-dev xml-core postfix
# postfix will prompt you for details
# use Internet Site and enter in the domain name you are planning on sending email from 

apt-get install apache2 apache2-prefork-dev libapr1-dev libaprutil1-dev libcurl4-openssl-dev git-core mysql-server mysql-client libmysqlclient15-dev libmysql-ruby
# mysql will also prompt you to set up a root user account. set the password to be anything you like

# next, download the latest release of ruby enterprise edition but when you're installing it on your own machine version numbers and release dates may have changed.
# pay attention to the version and release date before the file extension. it will be something like
# ... 1.8.7-2010.02
# this will change to something like 2011.03, 2011.04... etc in the future.
# just double check the paths on when you are installing and make the necessary substitutions

# ruby enterprise edition is available at http://www.rubyenterpriseedition.com/download.html
wget http://rubyforge.org/frs/download.php/71096/ruby-enterprise-1.8.7-2010.02.tar.gz
tar xzvf ruby-enterprise-1.8.7-2010.02.tar.gz 

./ruby-enterprise-1.8.7-2010.02/installer
# this may take a little while (just follow the instructions)
# and hit enter to install in default location (recommended) when prompted 

# and to install passenger (which is mod_rails for apache)
/opt/ruby-enterprise-1.8.7-2010.02/bin/passenger-install-apache2-module 

# i take the output from the above script and add it to my available modules directory
vim /etc/apache2/mods-available/passenger.conf

# and enter something like this in the newly created file (your version numbers will prob. be different)
LoadModule passenger_module /opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/gems/1.8/gems/passenger-3.0.2/ext/apache2/mod_passenger.so
PassengerRoot /opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/gems/1.8/gems/passenger-3.0.2
PassengerRuby /opt/ruby-enterprise-1.8.7-2010.02/bin/ruby

# and then sym link it to the enabled directory so that apache knows about it
ln -s /etc/apache2/mods-available/passenger.conf /etc/apache2/mods-enabled/passenger.conf

# and now i want to include ruby enterprise edition in my path so i add it to my profile (again make sure the path is correct)
vim /etc/profile.d/passenger.sh
export PATH=/opt/ruby-enterprise-1.8.7-2010.02/bin:$PATH

. /etc/profile.d/passenger.sh
# the "." file will make the setting available for the current terminal session
rails -v
ruby -v
rake -v
# should all be working now
# and
which ruby
# should point to the ruby enterprise edition under /opt

# next i
# set up public/private keys
# so i can do
# ssh localhost without using a password
cd
test -e ~/.ssh/id_dsa.pub || ssh-keygen -t dsa
cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys2

# and finally install git
apt-get install git-core

You should now have a server ready to server ruby on rails applications!

Installing and Using Rvm on Mac OS X, Creating Gemsets and Reverting to Original Environment

What is RVM and why should you use it? RVM is a Ruby interpreter, version management tool. In short, it enables you to switch between different versions and releases of Ruby (for instance, version 1.8.6, 1.8.7, jruby 1.9.2, ruby enterprise edition) on the same machine, while associating different gems with each version of the ruby interpreter. This is super useful and awesome. If you want to play with Rails 3 and Ruby 1.9.1, for 5 minutes, and then want to switch back to your production apps, which are running on Rails 2.3.5 and Ruby 1.8.7, you can do so with a single command from the terminal. With RVM this is a fairly simple process so there is no reason not to install it. You can also revert back to your system settings (not using RVM) with a single command. After all Rails is just a gem, so you can easily create and manage different RVM “gemsets”, (sets of different gems), for the different versions of Ruby (rubies as RVM refers to them) you have installed.

Installing RVM

bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

Next you have to add rvm to your bash profile

# place in ~/.bash_profile as the very last line
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

To check everything went well

type rvm | head -n1

Should tell you “rvm is a function”

How to add ruby, pass it the version to install

rvm install 1.8.7

*The current terminal session will load this environment. New sessions will not. To use a version of ruby and set it as the default, pass it the –default option

rvm use 1.8.7 --default

Next create a gemset, which will make available different gems for different versions

rvm gemset create rails_2_3_5

When you run “gem list”, you should see nothing!

gem install rails -v=2.3.5

Set a default rvm and default gemset, specify which gemset with the @ sign and include the –default option

rvm use 1.8.7@rails_2_3_5 --default
which gem
gem list
ruby --version
rails --version

And to get back to where you started and revert to using your original ruby setup

rvm system

For upgrading your version of RVM check out this post I wrote http://seanbehan.com/ruby/how-to-upgrade-rvm-on-mac-os-x/

Finally, you can create a .rvmrc file and put it in any directory and when you cd into that directory the environment specified in the file will be loaded automatically. This way you don’t have to remember the version and gemsets and type them into the console. All you have to do is put the ruby version and gemset name in the file like so

ruby1.8.7@rails2.3.5

You’ll be prompted to trust the .rvmrc file the first time, type “y” for yes. Also, subdirectories will inherit this .rvmrc so you can just put it in the parent directory like

rails2/
     .rvmrc
     app1
     app2
rails3/
     .rvmrc
     app1
     app2

And both app1 and app2 will use the .rvmrc environment while your rails3 directory apps will load the environment in its directory!

More information available here:
http://rvm.beginrescueend.com/rvm/install/
http://www.stjhimy.com/posts/4
http://eddorre.com/posts/installing-rails-3-beta-4-using-rvm