### Archive

Archive for the ‘Ruby’ Category

## Programming Language Rankings

The Tiobe index is total rubbish. But it’s worse than useless. It’s actually harmful. As Tyler Cowen mentioned in his TED Talk, “Be suspicious of stories”, feeling like you know what’s going on is way worse than admitting you don’t have a clue:

The most dangerous people are those that have been taught some financial literacy

Undoubtedly, there are people out there choosing careers and technologies based on the information age’s “divining by chicken bones”.  Tiobe is based on search engine hits for goodness sake. In the uber-democratic-everyone’s-a-technical-blogger web, of what is “talking about a language” a good indicator? Here is a primary one: that the language and its tools are not sufficient to support development. In other words, the Niobe Index (i.e. search engine results) is inversely proportional to language quality, as seen below:

$I_{n} \approx \frac{1}{Q_{l}}$
Since the equation above is obviously scientific (because it seems “mathy” and nerdy), it must be true. Furthermore (another great smart-people word), if one believes in (yes, like Santa Claus) the Niobe index (which can only be believed because it superficially seems scientific), one must believe my equation, which creates a paradox (another great science-y term).

In a live, open, dynamic environment (like Smalltalk*), the programmer has at their fingertips most of the things they would otherwise be forced to search the internet for, which is succinctly described by Torsten Bergmann. Also, working in a low level language, like C++ (e.g. manual memory management), guarantees search engine love. I used to need a Safari Books membership just to make sure I didn’t shoot myself in the foot.

* I am not pushing Smalltalk. In fact, I can’t wait until someone invents something better (hint, cough; VPRI).

Categories: Tags:

## Cocoa Smalltalk App

For a while, I’ve been thinking about using MacRuby for native UIs and multiple windows in Pharo on the Mac. I uploaded a proof of concept to https://github.com/seandenigris/PharoMacRubyTest . It has a simple Cocoa UI that uses http to get a value (the current time) from a running Pharo image and displays it in a text field. See the discussion on the Pharo mailing list.

Categories: Tags:

## MacRuby outlets don’t appear in Interface Builder (fixed)

Xcode 4.1 had a bug where IB did not recognize outlets in MacRuby classes. It is corrected in Xcode 4.2. However, if you upgrade to 4.2, you must then reinstall MacRuby for MacRuby outlets to start working again.

Categories: Tags:

## Love poor technology for simple tasks

##### I Have a Dream

If I had my way, I would never leave my Smalltalk image.

It’s not just the Smalltalk language (although I do enjoy the Zen-like minimal syntax, and keyword arguments flow off the keyboard like conversational speech). The real gold is the live, open environment, and control over everything in my system, almost down to the metal.

##### Reality Sets In (but it’s not so bad this time)

Yet sometimes, for a quick and dirty throwaway script, a tool symbolizing poor design is perfect.

For example, I wanted to change the artist info of a batch of tracks in iTunes. In less? time than it would have taken me to google a solution, I had whipped up an AppleScript (yes, ouch) in AppleScript Editor (double ouch):

tell application "iTunes" set wrongName to "" set rightName to "" set track_list to tracks of playlist "Music" of source "Library" whose artist is wrongName repeat with t in track_list set artist of t to rightName end repeat end tell

Yes, I could have used rb-appscript to script from Ruby, which I usually do. But, AE gave me easy access to iTunes’ scripting dictionary, a nice little “Run” button, and an output pane. For this tiny script, I was done before I started to suffocate from being outside the oxygen of my live, open Smalltalk image.

So, thank you Apple, for creating the infuriating AppleScript language, to be written with the puny AppleScript Editor, and for not even making many of your own applications scriptable ([cough] Preview.app) even while evangelizing scriptability to Mac devs in your docs. Today, I was grateful for it all. Editing info on hundreds of tracks was much quicker than writing this post about it

Categories: Tags:

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

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: Tags:

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

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

###### 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: Tags:

## Rails text_field_with_auto_complete and Formtastic

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

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

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.


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.

  %w{.git .svn .hg .swp .DS_Store ._* tmp}.each do |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!

Categories: Tags:

## Ruby, Vim, and Windows

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

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: Tags:
\n