Archive

Archive for the ‘Ruby’ Category

Adding Quicklook support for Ruby files with different/no extensions (e.g. rake files)

March 4th, 2010 Sean DeNigris No comments

1. Download the excellent qlcolorcode bundle from Google Code

2. Add the following line to QLColorCode.qlgenerator/Contents/Info.plist:

<string>public.data</string>
So that it looks like this:
3. In QLColorCode.qlgenerator/Contents/Resources/colorize.sh, add (and customize) the highlighted code, which you can get at github:
4. Oops!  Forgot, you may have to sudo touch /path/to/QLColorCode.qlgenerator for changes to take effect
Categories: Mac, Ruby Tags: , ,

Quicklook any plain-text file e.g. HAML or ERB

March 3rd, 2010 Sean DeNigris No comments

I’m often jumping from file to file in Rails, so I really wanted Quicklook support for my template files.  Unfortunately, the suggestions I found online were confusing and incomplete.

Here’s a really easy way to get it done – but no syntax highlighting :(

1. Show the contents of TextEdit’s package

2. Open Info.plist in a text editor… like TextEdit, perhaps… hmm…
3. Anyway, add the following snippet (find the code on github):


Put it just before the following section:

4. Customize
  • UTTypeDescription - put in description of your choice (although you probably don’t need to)
  • UTTypeIdentifier – apparently (and this is what was missing from other descriptions), you just make something up here
  • public.filename-extension – add as many extensions as you want to map to this file type
5. Gloat

Categories: Mac, Rails, Uncategorized Tags: , , ,

Rails text_field_with_auto_complete and Formtastic

March 1st, 2010 Sean DeNigris No comments

Formtastic is an awesome gem to make form creation easy and fun. Rails built-in autocompletion feature is also easy to use and a great service to users. However, they don’t play well together out of the box…

As you can see, the popup auto-complete menu and controls that follow are all jumbled up.  Luckily, the fix is very simple (after spending hours yelling at the computer, lol).  In formtastic_changes.css, add the following (get from github):

And everything’s right as rain…

Categories: Rails Tags: , ,

Reload Rails server to recognize plugins

March 1st, 2010 Sean DeNigris No comments

I’ve been following the excellent videos at Railscasts.com, but many times (especially after loading plugins or changing my layout files), I wouldn’t get the result from the video. What I found was that I needed to restart the Rails server to have the changes take effect. I remember for sure this happened while implementing the auto_complete text_field functionality.

Categories: Ruby Tags: , ,

Autospec with custom directory/file names

January 17th, 2010 Sean DeNigris No comments

Autotest is a great time-saver when doing BDD (or TDD for the less-enlightened, lol).  Autospec seemlessly integrates Rspec.  However, if you don’t use the expected “spec/*_spec.rb” structure, autotest will not find your specs – or examples, as I call them.

It is very easy to use whatever structure you want using the following steps:

Telling Autotest to use Rspec as the test runner

The first thing you want to happen is for Autotest to use Rspec as the test runner.  This usually happens automatically using autospec, which tests for the presence of the spec directory.  Since we do not want a spec directory, we must tell explicitly tell Autotest to do this.  So:

Step 1: in your project directory create a file autotest/discover.rb with the following line:

Autotest.add_discovery { "rspec" }

Teaching Autotest about our custom directory structure

In order to do this, we have two options.  If we want to use the same structure for all our projects, we can put a file in our home directory, otherwise we can add custom files to each project directory.

Step 2: create a file .autotest with the following code:


class Autotest::Rspec < Autotest remove_method :consolidate_failures def consolidate_failures(failed) filters = new_hash_of_arrays failed.each do |spec, trace| if trace =~ /\n(\.\/)?(.*example\.rb):[\d]+:/ filters[$2] << spec end end return filters end remove_method :add_options_if_present def add_options_if_present # :nodoc: File.exist?("examples/spec.opts") ? "-O examples/spec.opts " : "" end end Autotest.add_hook(:initialize) {|at| %w{.git .svn .hg .swp .DS_Store ._* tmp}.each do |exception| at.add_exception(exception) end at.clear_mappings # take out the default (test/test*rb) at.add_mapping(%r%^(examples|third_party)/.*_example.rb$%) { |filename, _| filename } at.add_mapping(%r%^lib/(.*)\.rb$%) { |_, m| ["examples/#{m[1]}_example.rb"] } at.add_mapping(%r%^examples/(example_helper|shared/.*)\.rb$%) { at.files_matching %r%^(examples|third_party)/.*_example\.rb$% } #nil }

Let’s take it piece by piece:


def consolidate_failures(failed)
  filters = new_hash_of_arrays
  failed.each do |spec, trace|
    if trace =~ /\n(\.\/)?(.*example\.rb):[\d]+:/
      filters[$2] << spec
    end
  end
  return filters
end

This method tells Autotest which test files have failed by pulling the file names out of the test output. Since autospec looks for *_spec.rb files, it will not find our custom files. I use *_example.rb filenames, so you should replace that part of the regex with whatever you use.


def add_options_if_present # :nodoc:
    File.exist?("examples/spec.opts") ? "-O examples/spec.opts " : ""
  end

This method tells Rspec where to find an option file. Again, replace the details with whatever you use.

In Autotest.add_hook(:initialize):

  %w{.git .svn .hg .swp .DS_Store ._* tmp}.each do |exception|
    at.add_exception(exception)
  end

This tells Autotest to ignore certain files that are irrelevent to testing

at.clear_mappings

This tells Autotest not to use any other mappings i.e. the default test/test*rb, or Rspec’s spec/*_spec.rb

at.add_mapping(%r%^(examples|third_party)/.*_example.rb$%) { |filename, _|
  filename
}
at.add_mapping(%r%^lib/(.*)\.rb$%) { |_, m|
  ["examples/#{m[1]}_example.rb"]
}
at.add_mapping(%r%^examples/(example_helper|shared/.*)\.rb$%) {
  at.files_matching %r%^(examples|third_party)/.*_example\.rb$%
}

This is the meat of the customization. It tells Autospec, based on which files have changed, which tests to run. Replace my regexes with your custom matches.

The end

Once you have added both these files, running autospec from your project directory will work as it should!

Ruby, Vim, and Windows

December 11th, 2009 Sean DeNigris 2 comments

It is unbelievably easy to create a wicked setup, although I spent half a day googling to narrow it down to the exsentials:

Downloads

  1. Ruby with the One-click installer.
  2. Vim’s self-installing Windows executable (this comes with many customizations you read about online, and ruby support, included)
  3. snipMate, a plug-in that automatically expands common Ruby constructs (e.g. you type ‘cla<tab>,’ which becomes class…end with placeholders!)
  4. AutoComplPop (must have!!!), an autocomplete plugin very much like Xcode’s – start typing and a popup list of completion options appears.  superTab, another plug-in that allows you to use tab to auto-complete Ruby code
  5. wombat, a great color scheme

Global configurations

Add the following settings to /path/to/vim/_vimrc.

note: the autocomplete menu colors, and vim’s default font are disgusting, hence the changes.

Ruby-specific Configurations

In the vimrc above, the following is added:

  • the first line sets up superTabs to look in the right place for Ruby completions (I use AutoComplPop now, see above)
  • adds line numbers to several source code file types
  • changes the ridiculous 8-space tabs to 2

That’s it!  Happy coding :)

Categories: Ruby, Software Engineering Tags: , ,

RubyCocoa and Interface Builder

December 4th, 2009 Sean DeNigris No comments

There isn’t much info out there about compatibility, but here’s the rundown after a few hours of experimentation and research:

  • MacRuby: fully integrated (according to Apple’s docs), so classes, outlets and actions automatically appear in IB
  • RubyCocoa
    • Nibs: there is a utility that adds classes, actions, and outlets to IB
    • Xibs: we’re out of luck, you must add items to IB manually
Categories: Ruby Tags: ,

BDD in the field

December 3rd, 2009 Sean DeNigris 2 comments

I am on fire about Ruby.  I always hated Objective-C’s weirdness (coming from C++), and read the most awesome idea recently – given the amazing power of modern computers (now pay attention, this is the good part): writing code in anything but the highest level, easiest language is… premature optimization!!!  The instant I read that statement (sorry to the author, I can’t remember where), I knew it was true.  Add that you can drop down from Ruby to lower level languages where you need extra speed, and that MacRuby and HotCoca are going to make it ridiculously easy to use Cocoa via Ruby and…

That’s it – I’m hooked.  To get myself up to speed in Ruby and Cocoa, I’m re-writing the examples from Aaron Hillegass’s awesome book Cocoa Programming for Mac OS X.  I’ll be writing about the intricacies of BDD in Ruby using Xcode, and I’ll be posting all the code, as well as custom file and project templates for Xcode.

Add missing Ruby file templates to Xcode

November 16th, 2009 Sean DeNigris No comments

If your file templates are missing from Xcode (I’m using 3.2.1), run the following commands in terminal, restart Xcode, and they will be there!

sudo mkdir /Library/Application\ Support/Developer/Shared/Xcode/File\ Templates/

sudo mkdir /Library/Application\ Support/Developer/3.0/Xcode/File\ Templates/

svn co http://svn.macosforge.org/repository/ruby/MacRuby/
trunk/misc/xcode-templates/File%20Templates /Library/Application\ Support/Developer/Shared/Xcode/File\ Templates/

sudo cp -r /Library/Application\ Support/Developer/Shared/Xcode/File\ Templates/* /Library/Application\ Support/Developer/3.0/Xcode/File\ Templates/

Categories: Ruby Tags: ,