Category Archives: Ruby on Rails Bits

Ruby on Rails Hittin’ The Big Time, A Friday PSA

How do you know that Ruby on Rails has already hit the big time?  Not that it needs anymore proof that it is absolutely one of the MAJOR platforms available right now…

  1. Recruiters now regularly come to user groups & offer to “buy the beer” afterwards.
  2. The split of job searches on sites now easily come up with dozens upon dozens of Ruby on Rails Jobs.
  3. Enterprise & Other Managers are commonly asking what the “Ruby on Rails Dev Base” looks like.  In other words, they want to know who and how many people they can hire.
Anyway that you look, you’ll see Ruby on Rails making inroads at a company near you! Keep your eyes peeled, and if you aren’t polyglot now, you might want to start thinking about it.
Cheers!  Happy disruptive markets to you!  😀

Keeping Your Rails Projects Organized Right!

I’ve been working with Rails now for about 3 months. At first I jumped right in like a bull in a china shop. I have since, suffered the frustration of doing so. I’ve experimented on OS-X, Windows, and Linux (primarily the Ubuntu Distro). Among these three operating systems, getting up and running with rails is a breeze. Sure, I’ve wrecked more than a few apps I started, blown to smithereens a few machine images, and been generally destructive – but that’s not a bad way to learn at all. 😉

Through this trundling, I’ve come to find there are a few things that should be reviewed and learned thoroughly before smashing into the china shop (i.e. rails or Ruby for that matter). One of these tools is RVM. Another is Git. These tools, without doubt or question, you MUST LEARN! There is just too much value in both of these tools to try to ignore either one. First a quick description of each:

Git – Git is a source control server and respective client software.
RVM – Ruby enVironment Manager – RVM, sometimes referred to as the Ruby Version Manager also, is a way to maintain the various gems and other environment settings that are used for a particular project. It enables switching back and forth between versions of ruby, keeping ruby updated, and much much more.  In .NET, think of it as choosing which version of .NET to use, except with more power to go beyond just merely choosing which version. These two tools are pivotal in having a smooth, consistent, and understandable workflow. There is one other issue for Windows users here though, RVM does not and will not ever run on Windows. One can however install cygwin to get it running or they can use Pik.


Below I have a short workflow tutorial, which I’ve broken out to getting started, working, and operational.

Getting Started

This is the first stage of any development project. Regardless of using PHP, Rails, .NET, Java, or whatever, there are certain things that need done. The key elements that I’ve found over the years include, not in any particular order;

  • Setup source control
  • Setup your environment
  • Start your basic project
After each of these things are done, and working together properly, it’s time to get coding. 🙂  First, setup a directory that will be the home for the entire project.
$ mkdir sampleWorkflow

Next run the command to setup your environment for this specific project.

$ rvm --rvmrc --create 1.9.2@sampleWorkflow
$ cd ..
$ cd sampleWorkflow/
= NOTICE                                                                     =
= RVM has encountered a new or modified .rvmrc file in the current directory =
= This is a shell script and therefore may contain any shell commands.       =
=                                                                            =
= Examine the contents of this file carefully to be sure the contents are    =
= safe before trusting it! ( Choose v[iew] below to view the contents )      =
Do you wish to trust this .rvmrc file? (/Users/adron/a_code/sampleWorkflow/.rvmrc)
y[es], n[o], v[iew], c[ancel]> yes
$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]

In the command above, rvm being the application, –rvmrc the parameter for what is being done, and –create is the command for the action to be taken. The 1.9.2 before the @ is the Ruby version and the value after the @ is the environment name, in this case being the same as the folder.

Once the command is run, move out and back into the folder to view how the rvmrc file will work. When you navigate into the directory again, the script will run, initiating the environment for Ruby 1.9.2. It will also ask to confirm if you trust the file. Running the ruby -v command to determine the version, will now display the active ruby version for this folder.

If you’re only using one version of Ruby, this might not seem that useful. But over time as you work with multiple projects, you will often find that different projects use different versions of Ruby. Sometimes 1.8.7 or jruby or rubinius. If that’s the case, rvm is a life saver in simplifying and keeping environments neatly organized.

Now that the environment is setup, we’ll need source control setup and as I generally prefer, an initial commit. Make sure to move into the directory that was just created. Issue the following git commands.

$ git init
Initialized empty Git repository in /Users/adron/a_code/sampleWorkflow/.git/

With our directory now intialized for git, it is best to get a git .gitignore file created. (I know, that’s a lot of get gittin). Use mate, or whatever your editor is you prefer, and create a .gitignore file.

$ mate .gitignore

At this point you’ll want to add something to the ignore file. I always start with the following basic files and folders to ignore. I’ve also written a short entry on what these files and folders are that I’m ignoring in the post Gotta Get Git.

#OS junk files

#Webstorm & Rubymine files

#Rails Heroku and other bits to ignore

Once that is entered, save the file and close. Next we’ll do our first commit. This is always a good practice, then there are no accidental commits of files that aren’t needed. Also note, I did not exclude the rvmrc file, this is needed to insure clarity about the environment when cloning the repository in the future.

$ git add -A
$ git commit -m 'first commit'
[master (root-commit) ea81bed] first commit
 3 files changed, 131 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 .rvmrc
 create mode 100644 .rvmrc.10.29.2011-11:33:32

Now that this is recorded in source control history (not of course in the main repo, we’ll do that in a second) I like to go ahead and create the rails application. Move to a directory just below where the ‘sampleWorkflow’ directory is and create a rails application named the same thing. Since we already created the .gitignore file, we’ll be prompted to overwrite, which isn’t needed since the .gitignore is already setup correctly.

$ ls
sampleWorkflow		someOtherAppOrSomething
$ rails new sampleWorkflow
      create  README
      create  Rakefile
    conflict  .gitignore
Overwrite /Users/adron/a_code/sampleWorkflow/.gitignore? (enter "h" for help) [Ynaqdh] n
        skip  .gitignore
      create  Gemfile
      create  app
      create  app/assets/images/rails.png
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css

…more stuff shows up…

 create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.gitkeep
      create  vendor/plugins
      create  vendor/plugins/.gitkeep
         run  bundle install
Fetching source index for
Using rake (
Using multi_json (1.0.3)
Using activesupport (3.1.1)
Using builder (3.0.0)
Using i18n (0.6.0)
Using activemodel (3.1.1)
Using erubis (2.7.0)

…more stuff shows up…

Using railties (3.1.1)
Using coffee-rails (3.1.1)
Using jquery-rails (1.0.16)
Using rails (3.1.1)
Using sass (3.1.10)
Using sass-rails (3.1.4)
Using sqlite3 (1.3.4)
Using turn (0.8.3)
Using uglifier (1.0.4)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

…and then when finished do a commit of the recent additions.

$ git add -A
$ git commit -m 'Adding Rails 3.1 Project.'

Now add the appropriate remote sources for git and the project is ready for development.

$ git remote add origin
$ git remote -v
origin (fetch)
origin (push)

The example, with extra example code in place, is available on @Github at


On a regular basis, while coding, one wants to commit their regular work and push those changes to the server (@Github in this scenario). After every change, add the changes to the commit, then commit with a message as shown. When done, do a pull to insure everything is up to date and then a push.

$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#	modified:   Gemfile
no changes added to commit (use "git add" and/or "git commit -a")
$ git add -A
$ git commit -m 'updated Gemfile.'
[master b06aa76] updated Gemfile.
 1 files changed, 0 insertions(+), 1 deletions(-)
$ git pull
Already up-to-date.
$ git push
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 289 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
   6986cd0..b06aa76  master -> master


This section, I’m going to primarily provide a bunch of links to specific instances of features for Git or RVM.

Git Branching

$ git checkout -b modify-branchSwitched to a new branch 'modify-branch'$ git branch  master* modify-branch

…make some changes…

$ git add -A
$ git commit -m 'committing a minor file change while in a branch.'
[modify-branch 726e770] committing a minor file change while in a branch.
 1 files changed, 3 insertions(+), 1 deletions(-)
$ git push
Everything up-to-date
$ git checkout master
Switched to branch 'master'
$ git merge modify-branch
Updating b967d4e..726e770
Fast-forward |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
$ git branch -d modify-branch
Deleted branch modify-branch (was 726e770).

Git Rebasing

Git Stashing

Managing Gemsets

“Working Directory doesn’t exist” in Rubymine! ARRRGGGHHH!!

So a few weeks back I created a Rubymine Ruby on Rails Project I was kicking off. I got it running, did some scaffolding, started customizing that for what I needed. I had created this project on Windows 7 and did not realize the implications of this. I did a clone via github of the code on a Mac via bash. I then opened Rubymine and opened the project. That’s when I got this error message, “Working Directory doesn’t exist”. I thought, well what the…   no reason for this. I’ve barely edited the project!!

I checked out the Jetbrains Forums and didn’t find an answer at the time, but did find others having the problem. Just today, Tyler Williams posted what had happened. Being that I don’t delete my projects, even slightly broken, for many days I went back to look at the .idea files as Tyler Williams suggested. Sure enough, my setting was hard coded (I suppose by the IDE??).

Which leads me to my recent thought that maybe I’ll be using TextMate more and Rubymine a little less. Even though, I do love the refactorings, code completion, and all that. But since I’m in the learning stage, and I’m doing hard core TDD (best I can with Ruby 🙂 ) I ought to not use the IDE as a crutch and instead force myself to learn the language well & the Rails Technology Framework! I’m getting there, but the battle still exists for me. At this point, I do my Ruby & Rails work about 1/2 in TextMate and 1/2 in Rubymine. Anyway, if you run into “Working Directory doesn’t exist” in Rubymine, now you have a good lead on what to do.

Last Night’s Cafe Racer Ruby on Rails Meetup; Kilt Lifter, EngineYard, Heroku and a Ruby Tone

Yesterday evening at the Cafe Racer Ruby on Rails meetupI knocked out these specific tasks.

Kilt Lifter by Pike Brewing Company

Kilt Lifter by Pike Brewing Company

  • Drank a solid tasty Kilt Lifter.
  • Created a sample beginning application, with standard migrations, and errata, and made two simple models with respective scaffolding.
  • Deployed the beginnings of a sample application to Heroku.
  • Deployed the beginnings of a sample application to EngineYard.

For Kilt Lifter information, click on the image at the right. If you live in the northwest, you know what’s up with the beer capital of the world (i.e. Portland, and the northwest in general, for info see here or click on the image to the left). We love our beer up here in these parts, we love it with a passion and it shows with more brewpubs, beer options, varieties, home brews, and other flavors than anywhere else in the world. No, I did not mis-state that, the northwest has more than anywhere else in the world, almost by an order of magnitude!  More than Germany, Ireland, England, the East Coast of the US, and the list goes on. So if you’re up for a beer and some coding, or just a beer, head to the northwest! We’ll have a few for ya to taste. 😉

The initial Ruby on Rails Application I created I started with RubyMine from Jetbrains. Simply a File -> New Project -> Rails Application, etc, etc. I worked through some of the issues with fellow rubiests and railers at the meetup. We did run into a confusing and nasty issue with Postgresql. Why was I installing Postgresql you ask? Well I was trying to do a deployment to Heroku, which provides a free shared instance of postgresql. I gotta admit, of all the things that Heroku makes absurdly easy, setting up a database is not one of them. Maybe I’m missing a bit, but I don’t know what the instance of the server is, what the username or password is, or anything of that sort. It isn’t all that intuitive that you have to add the database as an add-on either. At least, it wasn’t to the 3-4 of us that were working on it.

The other problem I ran into was what process I should actually use to install Postgresql. The firsts thing I tried to do was submit the “gem install pg” which ended with a completely wonky description. The error message read as:

$ sudo gem install pg
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
	ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb install pg
extconf.rb:1: command not found: pg_config --version
ERROR: can't find pg_config.
HINT: Make sure pg_config is in your PATH

I found a fix, here put together by Graham Ashton. Thanks Graham!

The fix involved simply to add a path variable…

$ PATH=$PATH:/Library/PostgreSQL/8.3/bin sudo gem install pg

Meanwhile I dived into EngineYard and got the application up and running in short order. I have to say I am much impressed by the increased visibility into what’s going on with EngineYard. Heroku seems to hide just a bit too much. Add to that their over-reliance on AWS East and not geographically dispersing the platform, I’m leaning even heavier on suggesting EngineYard for startups and companies that want a cloud provider platform to build to.

Once the applications I’ve been working on get to a certain state, I’ll be providing a write up regarding the applications. However, until then, feel free to follow me on Twitter and Github or jump in fork my code and send me a pull request.  🙂

Update: Just some images from the meetup.

Ruby on Rails meetup at Cafe Racer - Corner Table

Ruby on Rails meetup at Cafe Racer - Corner Table

Ruby on Rails Meetup at Cafe Racer

Ruby on Rails Meetup at Cafe Racer

Bash, Ruby, and Such Console Installation Version Management Bits for OS-X

I went to reinstall RVM (Ruby Version Manager) and got a message that the command wasn’t available. I realized, after fiddling around for a while that I didn’t have bash setup, and I had tried to run the standard quick install of:

bash < 

…I had not setup bash shell as my shell on this Mac?! Oops. So since I’m a virtual noob sometimes, and I have no idea how to setup bash, I did some searching and came up with this solution.

First, switch to bash.

chsh -s bash

Then create yourself a .bash_profile so that you’ll be able to initiate variables and such that you’ll need in the shell. These are used when working with Ruby, Rails, and a whole host of other things. To create a .bash_profile you can follow these commands.  (Snagged from this find)

cd ~/
touch .bash_profile
open -e .bash_profile

Then add this to the bash file.

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

Now run the install (usually you’re told to setup this bash file afterwards, but figured we might as well get it done).

bash < 

Next exit (or whatever your restart of bash technique is) and open your bash terminal back up. Type this command to be sure we’re on the right path.

type rvm | head -n1

Now to install the latest Ruby on Rails bits you can issue a command like this.

rvm install 1.8.7

However, OS-X already comes with 1.8.7, so you’d probably want to install 1.9.2. I issued the 1.8.7 install since I knew, previously, I’d dorked up the install a few days ago. So it is also a good way to make sure your install is cleaned up. I’m sure though, it could possibly mess things up if you’ve tweaked things the wrong way – but it sure seems to be the case that Ruby, Rails + RVM just works. 🙂

rvm install 1.9.2

Once those are installed you’ll want to set one as default. I’m went with 1.9.2 for now. This can be changed later if need be.

rvm use 1.9.2 --default

Anyway, that’ll get you started on OS-X if you’ve run into the “I’m only running the Terminal in its default setup, I need some bash” situation. 🙂

Happy coding, hacking, and gem installing!


Some references are included above as links, but these below didn’t fit exactly in context at any point, I however used them none the less.

A Zillion Ruby Kōans

NOTE: Because of formatting, I couldn’t have a curly bracket with a colon and an “o” or it turns up with some unavailable WordPress Emoticon URI. So thus I have changed items with this naming to :z_one and :z_two respectively.

Yup, still moving through all the Ruby Koans. There are, after all 276 of these things! 😉 So no more yapping, onto the notes and green lighting the tests. Cheers!

def test_creating_hashes
  empty_hash =
  assert_equal Hash, empty_hash.class
  assert_equal({ }, empty_hash)
  assert_equal 0, empty_hash.size

def test_hash_literals
  hash = { :z_one  => "uno", :z_two => "dos" }
  assert_equal 2, hash.size

def test_accessing_hashes
  hash = { :z_one => "uno", :z_two => "dos" }
  assert_equal "uno", hash[:z_one]
  assert_equal "dos", hash[:z_two]
  assert_equal nil, hash[:doesnt_exist]

From test_creating_hashes a or {} both appear to create a new empty hash object. The second test creates and adds values to the hash. The third test has asserts that verify the data is inserted in the positions that are expected.

def test_changing_hashes
  hash = { :z_one => "uno", :z_two => "dos" }
  hash[:z_one] = "eins"

  expected = { :z_one => "eins", :z_two => "dos" }
  assert_equal true, expected == hash

  # Bonus Question: Why was "expected" broken out into a variable
  # rather than used as a literal?

Again, the hash is setup into the hash variable. Then the expected is setup in another variable. Since Ruby is pointer oriented, the expected variable is setup identical to the hash variable to assure that they truly are identical.

def test_hash_is_unordered
  hash1 = { :z_one => "uno", :z_two => "dos" }
  hash2 = { :z_two => "dos", :z_one => "uno" }

  assert_equal true, hash1 == hash2

def test_hash_keys
  hash = { :z_one => "uno", :z_two => "dos" }
  assert_equal 2, hash.keys.size
  assert_equal true, hash.keys.include?(:z_one)
  assert_equal true, hash.keys.include?(:z_two)
  assert_equal Array, hash.keys.class

def test_hash_values
  hash = { :z_one => "uno", :z_two => "dos" }
  assert_equal 2, hash.values.size
  assert_equal true, hash.values.include?("uno")
  assert_equal true, hash.values.include?("dos")
  assert_equal Array, hash.values.class

test_hash_is_unordered shows that a hash, no matter the order the values are assigned, assigns them to the values that are directly set to.

def test_combining_hashes
  hash = { "jim" => 53, "amy" => 20, "dan" => 23 }
  new_hash = hash.merge({ "jim" => 54, "jenny" => 26 })

  assert_equal true, hash != new_hash

  expected = { "jim" => __, "amy" => 20, "dan" => 23, "jenny" => __ }
  assert_equal false, expected == new_hash

This test asserts that the two different hashes are different, nothing amazing or odd there. The second part asserts that the next and expected hashes are different. Which again, is what we expect.

def test_default_value
  hash1 =
  hash1[:z_one] = 1

  assert_equal 1, hash1[:z_one]
  assert_equal nil, hash1[:z_two]

  hash2 ="dos")
  hash2[:z_one] = 1

  assert_equal 1, hash2[:z_one]
  assert_equal "dos", hash2[:z_two]

This test confirms that a hash with a request against a hash position that isn’t assigned to yet returns nil. The later two asserts show that a number compared to the hash position that contains a number compares as a number, and a string compared to a position that has a string also compares as equal.

Until next time, hack some koans.

Kōans of Code

I’ve continued the Kōans, but there is one thing that won’t be noticeable from this blog entry. I however wanted to mention it. The first blog entry was worked through on an OS-X Apple Machine, the second on an Ubunta Linux Machine, and now I’m heading for this blog entry to be completed with  Windows 7. It is of course completely irrelevant, but at the same time very relevant.  🙂 But enough about operating system awesomeness. Let’s take a look at the new gazillion Kōans that caught my note taking.

class AboutArrays < EdgeCase::Koan
  def test_creating_arrays
    empty_array =
    assert_equal Array, empty_array.class
    assert_equal 0, empty_array.size

Easy peasy. creates a new empty array. Got it. An Array is the class type retrieved when calling empty_array.class. Got it. The empty array has a size of zero, Got it.

  def test_array_literals
    array =
    assert_equal [], array
    array[0] = 1
    assert_equal [1], array
    array[1] = 2
    assert_equal [1, 2], array
    array << 333
    assert_equal [1, 2, 333], array

Ok, this one has some interesting bits in it. Having array[0] and array[1] assigned to a value of 1 and 2 seems standard operating practice for a language. This dual chevrons pointing into the array thing is a little more unique. This operator appears to take the value on the right, and put it onto the array’s stack. Simply, the double kick operator (or whatever it is called) puts the value on the next available array index position. Ok, cool. Got it.

  def test_accessing_array_elements
    array = [:peanut, :butter, :and, :jelly]
    assert_equal :peanut, array[0]
    assert_equal :peanut, array.first
    assert_equal :jelly, array[3]
    assert_equal :jelly, array.last
    assert_equal :jelly, array[-1]
    assert_equal :butter, array[-3]

Alright, got an array with 4 elements in it. First assert confirms that :peanut is the returned value from the first element in the array. The array.first call functionally does the same thing. The third assert gets the 3rd value in the array, keeping in mind a zero based index for the array, that gives us the 4th actual item in the list of items stored within the array. I love it, standard programmer weirdness. Why do programmers start with zero when nobody on earth starts a “list” of items with a zero. Blagh, whatever, that’s the reality of it. (That was, in a sense, somewhat rhetorical, I get the underlying reasons but that doesn’t help explain a zero based index to initial non-programmers.)

  def test_slicing_arrays
    array = [:peanut, :butter, :and, :jelly]
    assert_equal [:peanut], array[0,1]
    assert_equal [:peanut, :butter], array[0,2]
    assert_equal [:and, :jelly], array[2,2]
    assert_equal [:and, :jelly], array[2,20]
    assert_equal [], array[4,0]
    assert_equal [], array[4,100]
    assert_equal nil, array[5,0]

Slicing arrays again with the PB & J. With two values, the first thing I notice is that this is no multidimensional array. Two values within the square brackets means that you have a starting position and then a value of how many to retrieve. If there is a value like the fourth asset, starting at the 2nd position (3rd actual value) and taking the next 20 elements, basically retrieves whatever values are available, which in this case gets us 2 elements.

Now, I’m a slight bit perplexed though as to why nil is returned for something request nothing from the 5th starting point of the array versus the same being requested from the 4th starting point in the array. I’ll have to read up on that…

  def test_arrays_and_ranges
    assert_equal Range, (1..5).class
    assert_not_equal [1,2,3,4,5], (1..5)
    assert_equal [1,2,3,4,5], (1..5).to_a
    assert_equal [1,2,3,4], (1...5).to_a

Again, identifying the class object type is easy, a range of numbers is a Range Object. Check. A range stating 1..5 does not provide the numbers one through five. Calling to_a on a range however does provide you those numbers. Doing the same thing to an array specified with three periods instead of two with the to_a provides numbers one through four. That seems odd, but I got it.

  def test_slicing_with_ranges
    array = [:peanut, :butter, :and, :jelly]

    assert_equal [:peanut, :butter, :and], array[0..2]
    assert_equal [:peanut, :butter], array[0...2]
    assert_equal [:and, :jelly], array[2..-1]
    assert_equal [:peanut, :butter, :and], array[0..-2]

Slicing an array of four values, stating the starting and ending point with the syntax of a range, provides the elements based on the values associated with that range. I actually added an assert to this test to determine what exactly the negative values do. It appears that the array starts at the point of the first number, then follows a range from that until the negative number from the end of the array. So with 10 items, starting at point 2 and ending -2 from the end will retrieve the middle 6 elements. Strange, but I can see how this would be very useful.

  def test_pushing_and_popping_arrays
    array = [1,2]
    assert_equal [1,2,:last], array
    popped_value = array.pop
    assert_equal :last, popped_value
    assert_equal [1, 2], array

Pop. Easy, get that last value. But wait a second, the array itself doesn’t have :last in it? Aha! Popping it not only gets that last value, but literally takes the value out of the array.

  def test_shifting_arrays
    array = [1,2]
    assert_equal [:first, 1, 2], array
    shifted_value = array.shift
    assert_equal :first, shifted_value
    assert_equal [1,2], array

Ah, kind of like a popped value but shifted out of the array? Weird, this is a little confusing at first. I see what it is appearing to do, but figured a good read of the documentation would be good. I did a search on Google and the first hit is someone asking this question.

What does Ruby’s Array Shift Do?

From that quick read it appears that shift and unshift are used similar to pop and push in a stack (ala git, etc).

That answers that question. With that, I’m off to other realms.