27 November 2012

ruby day one

by mo

Ok so let’s review the first day.

We started off by discussing goals, personal and professional. Then we worked on the movie library exercise. We spent some time trying to complete the exercise then we began refactoring a little bit.

We implemented a custom RSpec matcher.

  module RSpec
    Matchers.define :contain do |*expected_items|
      match do|results|
        expected_items.each do |item|
          results.include?(item).should be_true

    Matchers.define :contain_in_order do |*expected_items|
      match do|results|
        expected_items.should == results

Then we spent a little bit of time talking about load paths and coming up with a file loading strategy that allowed us to move files around at will without having to constantly update our load strategy.

We used irb to understand $LOAD_PATH and $:. $: is just a short hand for $LOAD_PATH We talked about unshift is just a method on Array that adds an new item to the start of an array. So when you $:.unshift something you’re actually just adding a new path to the start of the load path array so that items from that path are used first.

We came up with a strategy where we create convention of adding any sub directories of our lib folder to the load path.

  Dir.glob('lib/**/*.rb').each{|item| $:.unshift(File.dirname(item))}

  Dir.glob('lib/*.rb').each do |f|
   require File.basename(f, '.rb')

We learned how to adapt blocks and specifications using modules and block_given? We created a specification contract to have a matches? method. We created the CompositeMatch class. We created a matcher module that can be mixed in to our specification to be able to add additional behaviour like “or”.

We later discussed recording macros in vim and pasting them into a file to be able to edit the macro. JP says he has scripts and conventions that he uses to read macros in from a file that he can reuse throughout his projects.

We then moved on to build a internal dsl for building queries. We started building the Where class as our gateway to building a query api.

  it 'should be able to find all movies published by pixar' do
    results = sut.all_items_matching Where.item(:studio).is_equal_to(ProductionStudio.Pixar)
    results.should == [ cars, a_bugs_life ]