Rails Development Environment
I’ve experimented with different Rails development environments over the years. I started with an Emacs/shell based environment. Then switched to TextMate because I had a Mac and that seemed to be what all the cool kids were using. Then I switched to Netbeans because of it’s Rails support and the amount of Java development I was doing at the time. (I’m not very smart. I prefer to conserve my limited intellectual resources by sticking to one editor) . Unfortunately Netbeans stopped supporting Rails and Rails 3 so I went back to Emacs and the shell and I’m very happy I did. The advantage of the other tools I mentioned is there is a short learning curve to get up to speed compared to the Emacs based system I use. IMHO the Emacs/shell bashed system is well worth the extra effort involved in getting up to speed. Even if you opt to stick with your GUI based tools it can be very helpful to have a shell based environment in the event you have to do production support on *nix servers that typically don’t have a GUI. I hope that this post will make it easier for you to become productive in a shell based development environment.
I like my current setup for a number of reasons. First, I’ve learned you can save yourself some unpleasant surprises if you develop on the same OS you deploy to. Like most Rails devs, I deploy to Linux so I like to develop on Linux. Unlike TextMate, Emacs runs on Linux, and unlike Netbeans, you don’t require a GUI to run emacs. Note that Emacs runs fine on Windows, Mac, Linux, AIX, Solaris, and BSD. No other development tool I know of save Vi does the same. I find I’m a lot more productive if I can keep my hands on the keyboard and avoid interrupting my work flow by reaching for the mouse. My shell based development environment helps me keep my hands on the keyboard.
OK, so here’s how I set everything up. You’ll need to have GNU Screen, RVM, and of course Emacs installed on your system. Emacs is your code editor of course. RVM is a tool that I use to create a development sandbox with explicit ruby/jruby versions and gemsets, pretty much required if you work on multiple Rails projects. The last thing is GNU Screen. From the GNU Screen page, “Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.”. I use screen because it lets me easily hot key through several shell screens as I work. For Rails, I like to have the following:
- A shell running the Rails server
- A shell where I tail the development log
- A database console or mongodb shell
- A command prompt
- Emacs running in nox mode
- The Rails console
To get all this set up you’ll need to write some shell script. Note that to my knowledge, the script I provide only works on Linux with Bash. If you are using some other shell or OS you’ll need to adapt the script to use your shell and OS. I add this script at the end of .bashrc. The script will cause a menu to be displayed (See Below) when you open a shell. You can select from your projects which in my example are hireme, naiku, and swap. When you select a project from the menu RVM is invoked setting up the Ruby interpreter and the Gems you’ll be using, the directory is switched to that of the selected project and screen is kicked off setting up the several shell windows according to your preferences. Each project has a file in it’s root directory called screen-startup-commands that is used to control the tools and shells that will be launched. (See Below). When the script is finished you’ll have Screen running several windows with all the stuff you need to develop productively in Rails.
Script
# this goes at the end of .bashrc
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
[ -n "$RAN_ONCE" ] && return
echo "========================================="
echo " Select Development Environment "
echo "========================================="
select selection in swap hireme naiku shell; do
export RAN_ONCE='y'
case ${selection} in
shell ) break ;;
swap )
. rvm use ruby-1.9.2@swap
cd ~/code/swap/ssrails
screen -c screen-startup-commands
exit 0
;;
hireme )
. rvm use ruby-1.8.7@hireme
cd ~/code/hireme
screen -c screen-startup-commands
exit 0
;;
naiku )
. rvm use ruby-1.9.2@naiku
cd ~/code/naiku
screen -c screen-startup-commands
exit 0
;;
esac
done
Startup Menu
Screen Startup Commands
# screen startup commands
screen -t emacs emacs -nw .
screen -t console rails console
screen -t log tail -f log/development.log
screen -t shell
screen -t server rails server





