Tag: Ruby

January 2012 Ruby on Rails Stack

Posted by – January 10, 2012

It seems like I am, entirely too frequently, setting up a new development environment for Ruby on Rails. Between the computers at work and at home I feel like I am starting over, from scratch, somewhere every 6 months or less. Whenever I make a substantial change to my development environment, I like to document it here for my own reference and in the hopes that it may help others.

This go around I am incorporating some recent additions to the stack, notably Homebrew and rbenv. These additions, as well as a few other changes are being made to simplify things and to follow the latest best practices.

The steps I followed to setup my development environment are as follows:

1. Install Homebrew – the missing package manager for OSX. Luckily I didn’t run into any issues installing Homebrew. Homebrew greatly simplifies installing some of the more difficult bits and pieces on OSX.

2. Install rbenv – This differs from my previous use of RVM. While I really liked RVM, the simplicity of rbenv convinced me to switch. Again, no real issues with installation (that is the beauty of a single purpose, simple tool). I didn’t use Homebrew to install rbenv, but in the future I will. With rbenv I also installed the companion ruby-build which simplifies the process of installing various Ruby versions.

3. Install PostgreSQL – While I am a longtime fan of MySQL, it’s acquisition by Oracle combined with the herd moving towards Postgres made me take the plunge. All in all it has been a fairly straightforward process. There is one caveat, when installing Postgres on OSX Lion (which replaced it’s bundled install of MySQL with Postgres) you need to make a minor change to the system paths so that Postgres loads from your installation and not the built in one. I followed the tips from a few blogs and updated /etc/paths, placing “/usr/local/bin” at the top. That one tip, combined with following the instructions provided by Homebrew (accessible via “brew info postgresql”) will get Postgres up an running in no time.

That is basically it! I think this is a really clean and simple install that makes my apps fairly portable. I am using Bundler to manage my Ruby Gems in conjunction with the rbenv-gemset plugin. I could just rely on Bundler, but I prefer to have the gems sandboxed in a separate gemset (for now).

After cloning from GitHub and running “bundle install” and the various rake database tasks I was up and running in development mode with relative ease. One of the wonders of the open source software community is that there seems to be a real emphasis on continuous and meaningful improvement. With each major iteration the tools I am using are becoming both more powerful and easier to use. I was able to get my development environment up and running in about an hour (less if my memory were better). That pales in comparison to some of the herculean efforts required in the past.

Sinatra + Dreamhost

Posted by – October 26, 2010

I was banging my head against a wall yesterday trying to get a site up and running on Dreamhost. The site was created using Sinatra which was relatively painless and straightforward.

I followed a few tutorials, all to no avail:

So, what did I learn? Dreamhost doesn’t necessarily have the gems you need, or the dependencies for the gems you need. What I needed to do was to “vendor” a copy of Sinatra, but not the most recent version which has a dependency for a gem called “tilt”. So I took a stab in the dark, picked an older version (0.9.4) and after some de-hacking from previously failed attempts to get things working the site is up and live using Sinatra on Dreamhost.

My final configuration is very similar to the first to tutorial links above, the key part for me was vendoring the correct version of Sinatra.

Ruby on Rails, Passenger & MySQL PATH issues in Snow Leopard

Posted by – September 8, 2009

I recently did a clean installation of Snow Leopard, which is great by the way, and I ran into some issues after installing MySQL and Ruby on Rails based on the instructions from Dan Benjamin at Hivelogic.

Specifically, when I would try to access a site in development mode I would get an error from Passenger that “Rails 2.3.4 could not be found”. I also noticed that on reboot my PATH wasn’t being loaded properly either so the system could not find MySql, Ruby, or RubyGems.

I tried placing the path information that, according to the instructions goes in ~/.profile into my ~/.bash_profile.

Now when I restart the system, MySQL and Ruby on Rail can be found! Passenger is working now as well. I am not sure if there was a change in the way Snow Leopard loads bash profiles or if there is just a problem with my installation that was preventing it from working when the PATH was specified in ~/.profile, but it’s all working now so hopefully this will help someone else.

Make OSX Terminal pretty

Posted by – August 31, 2009

I have always been annoyed with how a new tab opens in the OSX Terminal. I have my Terminal set to run “source ~/.bashrc” when the application starts. The problem with this is that it won’t run for new tabs, and I use lots of tabs (Rails development, web server, database, ruby console, etc…). And while I have been annoyed, it never really bothered me enough to look for a solution, until today.

As is usually the case, a quick search in Google led me to “Customizing the terminal prompt on Mac“. Apparently, OSX handles the terminal profiles a little differently than what I am used to. OSX uses profiles, where Ubuntu uses .bashrc. So, just add one line in your terminal profile to tell the terminal run the necessary command to load your .bashrc like so:

  1. First open up your ~/.bash_profile in your text editor of choice
  2. Add the line “source ~/.bashrc”
  3. Save and close ~/.bash_profile

Easy as can be! Now set your .bashrc to make your terminal as color-coded and informative as you want it to be. There are a lot of tutorials and information available for editing your .bashrc file.

Updating Ruby on OSX Leopard

Posted by – May 13, 2009

Quick and easy method for updating, or rather installing an updated version of Ruby on OSX Leopard (10.5) from Hivelogic. Ruby ships with OSX Leopard which is great, but the version included is outdated in regards to the recommendations of the Ruby on Rails folk.

This method seems to be the best way because it doesn’t overwrite what Apple has shipped with OSX, rather it just tells the computer to keep the older version, but use the newer version in it’s stead. Of course, this does require some command line shenanigans, but once you get started it really isn’t all that difficult. Besides, if you are concerned about using Ruby and/or Rails you should be comfortable using the command line!

Some Git goodness

Posted by – May 11, 2009

A project I am working on has a configuration file that contains a few variables for using Microsoft’s Active Directory to authenticate to a Ruby on Rails application. This is working great, but I was banging my head against a wall trying to get Git to not track changes to the configuration file. I didn’t want my username and passwords tracked in GitHub for obvious reasons, but I did want a generic copy of the configuration file in the repository.

While searching for a solution today, I ran across this little gem of an article titled “GIT: ignoring changes in tracked files” from Pagebakers.nl, the salient code is below:


git update-index --assume-unchanged <file>

Add weekdays in Ruby on Rails

Posted by – March 9, 2009

I am working on an application where I need to be able to add 3 week days to today’s date. Unfortunately there doesn’t seem to be any built-in functionality for this in Ruby, so I needed to roll my own. At first I searched Google hoping someone had already solved the problem for me, which is most often the case. Not so with this problem. I did however come across this post on Platte daddy, which provided a good starting point, but was designed to simply add 1 weekday, not any number of days that I want.

I ran through a number of iterations until I came up with this helper method:


def add_cwdays(date, n)
#Add number of weekdays plus necessary weekend days
date += n.days + (2*(n/5.floor)).days
#If the above lands on a weekend, keep adding days until it is a weekday
date += 1.days until (1..5).member?(date.wday)
date
end

This was added to my ApplicationHelper.rb file and is called for adding 3 week days to today via:


add_cwdays(Time.now, 3)

I created a helper to DRY the method and I wrote it so that the starting date and the number of workdays is variable. The helper has only 2 lines of code (after some re-factoring), which reduces the readability a bit, but it still makes sense I think.

The first line simply adds the desired number of weekdays, plus any weekend days that would be within the range of week days. If we wanted to add 8 weekdays, we would need to add 10 total calendar days (1 weekend). If we wanted to add 23 weekdays, we would need to add 31 total calendar days (4 weekends). If we wanted to add 4 weekdays, we would need to add 4 total calendar days (no weekends). The weekend addition is made with the (2*(n/5.floor)).days bit of code. Basically take the number of days “n”, divide by five, strip off the decimal and multiply by 2 (for Saturday and Sunday).

The second line of code deals with the eventuality of the first line landing on a weekend. If this happens, we simply add 1 day repeatedly “until” we reach a weekday.

This seems to be working well, if anyone looks this over and sees anything wrong or if there are any opportunities to re-factor and simplify, please post comments.