Literally 101 Ruby tricks

The linked slidedeck holds many tips, of which I list the most interesting to me below


The __END__ keyword tells Ruby where a file ends – but you don't have to stop there. Any text you add after it is accessible via the DATA object.

Running the attached example file data.rb prints:

DATA is a File object to everything in the current file after "__END__"

No braces needed for 'special variables'

@instance, @@class, $global = [ 'instance', 'class', 'global' ]
puts "#@instance, #@@class, #$global" ...

Rspec_spinner or cucumber_spinner get into an infinite loop

Chances are, something in your project uses the mathn library, which is known to cause infinite loops when rendering the progress bar. If so, this will probably fix it.

External content

A jQuery plugin pattern every jQuery plugin should use

Many jQuery plugins suffer from a good plugin architecture. When you write jQuery plugins you should use the plugin pattern described in this article which makes your plugin highly customizable and extensible.

Related article with patterns for namespaced jquery plugins (more detailed)

Asset pipeline may break Javascript for IE (but only on production)

If some of your JavaScripts fail on Internet Explorer, but only in staging or production environments, chances are that JavaScript compression is the culprit.

By default, Rails 3.2 compresses JavaScript with UglifyJS. I have seen a few cases where this actually breaks functioning JavaScript on IE (one example is the CKEditor).

I fixed this by switching to Yahoo's YUI Compressor.

To do this, do the following:

  • replace the uglifier gem with the yui-compressor gem…

Unsaved record disappears when assigning to an association

If this happens to you:

user.avatar =
user.avatar  # => nil

(where avatar is a belongs_to), you probably declared your association incorrectly.

Always do

class User < ActiveRecord::Base
  belongs_to :avatar

and never

class User < ActiveRecord::Base
  belongs_to 'avatar'

Migrating to Spreewald

This describes how to migrate an existing cucumber test suite to Spreewald.

  1. Add the gem
  2. Include spreewald into your cucumber environment by putting
    require 'spreewald/web_steps'
    require 'spreewald/email_steps'
    # …
    or just
    require 'spreewald/all_steps'
    into your support/env.rb.

  3. Look through your step definitions for everything that might be included in Spreewald. Candidates are web_steps, shared_steps, table_steps, `em…

How Ruby lets you keep script and data in *one* file

The __END__ keyword tells Ruby where a file ends – but you don't have to stop there. Any text you add after it is accessible via the DATA object.

The attached example file data.rb looks like this:

DATA is a File object to everything in the current file after "END"
Running it with ruby data.rb prints:

DATA is a File object to everything in the current file after "__END__"

Workaround for broken integer division after requiring the mathn library

Ruby's mathn library changes Fixnum division to work with exact Rationals, so

2 / 3 => 0
2 / 3 * 3  => 0

require 'mathn'
2 / 3 => Rational(2,3)
2 / 3 * 3 => 2

While this might sometimes be quite neat, it's a nightmare if this gets required by some gem that suddenly redefines integer division across your whole project. Known culprits are the otherwise excellent distribution and [GetText](https://g…

Get rid of WARNING: Nokogiri was built against LibXML version 2.7.7, but has dynamically loaded 2.7.8

Rebuilt the gem with the newer library

gem install --no-rdoc --no-ri nokogiri -- --with-xml2-include=/opt/local/include/libxml2 --with-xml2-lib=/opt/local/lib

If you still get the error, try to uninstall all nokogiri versions with

gem uninstall nokogiri

and install nokogiri again.

Cucumber step to interact with an iframe using Capybara webdriver

Give your iframe a name attribute (i.e. <iframe name="myframe">) and then simply use

When I press "Foo" in the iframe "myframe"
Then I should see "Bar!" in the iframe "myframe"

Step as follows:

Then /^(.*) in the iframe "([^\"]+)"$/ do |step, iframe_name|
  browser = page.driver.browser

exception_notification: Send exception mails in models

You're using exception_notification and want to send exception mails within a model. Here's how.

The ExceptionNotifier class has a method notify_exception for that. Simply pass an exception:


=> #<Mail::Message:77493640, Multipart: false, Headers: <Date: Mon, 24 Sep 2012 13:37:00 +0200>,
<To: ["", ""]>,
<Message-ID: <5060543b3759_212311986a0305e...

How to not leave trailing whitespace (using your editor or Git)

There is no reason to leave trailing whitespace characters in your project's files, so don't add any.

A git diff --check will tell you if there are any and you should not commit when you see them. So go ahead and switch your editor/IDE to automatically remove them for you.
Below are a few instructions on how to get them removed by your favorite IDE or editor.

Note that except for RubyMine, the following changes will remove trailing white-space on all lines, not only those that you changed.
While this should not be a problem if your proje…

External content

Creating a Grid for Product View for a eCommerce Site

Randy Hoyt is presenting a way to display something (e.g. products) in a grid. It's responsive, too!

External content

An easy demonstration of what Twitter Bootstrap does

Holly Schinsky from Adobe shows some of Bootstrap's capabilities. The combination of explanation, screenshots and source code makes it easy to understand

Howto transfer a single mysql table between several deployment stages

Example task: Multiply the table holidays between several stages.

  1. Open two terminals:

    shell-for stage_1
    shell-for stage_2
  2. Get the stage1 and stage2 MySQL credentials:

    cat /opt/www/
    cat config/database.yml # should do it
  3. Dump the table to a path reachable by the stage2 user (e.g. home):

    mysqldump -h mysql1 -u stage_1_user -p stage_1_database table_name > ~/table_name_dump.mysql # Select certain records using --where "some_id > 666"


Machinist blueprints do not work with a column :display

This didn't work for me. Seems display is already taken in Machinist.

# in spec/support/blueprints.rb
Partner.blueprint do
   display { true }

Git: How to look at the stash

Browsing the git stash is a bit tricky. Here is how to see the changes without applying them:

git command on the console

The following will give you the diff of the topmost stash item:

git stash show -u

But what about other items on the stash?
Well, you can list them like this:

$ git stash list
stash@{0}: WIP on feature/foo
stash@{1}: WIP on feature/bar
stash@{2}: WIP on fix/baz

All those stashed changes have their own reference (like branch names) that you can look at, like `stas…

Using before(:all) in RSpec will cause you lots of trouble unless you know what you are doing

TL;DR Avoid before(:all), use before(:each) instead.

If you do use before(:all), you need to know what you are doing and take care of any cleanup yourself.


Understand this:

  • before(:all) is run when the context/describe block begins,
  • before(:all) is run outside of transactions, so data created here will bleed into other specs
  • before(:each) is run before each spec inside it,

Generally, you'll want a clean setup for each spec so that they are independent of other specs in the same context.


3248 cards