Silencing Deprecation Warnings in Rspec

Deprecated Ruby tests can flood RSpec output with warning noise. A narrower way to quiet them avoids hiding warnings from dependencies.

Iterate over every n-th element of a Range in Ruby

Use Range#step to visit every n-th value in a Ruby range, useful for date sequences such as weekly Mondays. Rails and ActiveSupport return an array when no block is given.

Upgrade from Rails 2.3.8 to Rails 2.3.10

Rails 2.3.8 to 2.3.10 upgrades can reduce bugs and security risk in older applications.

Apache: Log the original client IP when your site sits behind a reverse proxy

Apache logs behind a reverse proxy show the proxy IP instead of the original client IP, complicating troubleshooting and traffic statistics.

Test a download's filename with Cucumber

Verify that a server response suggests the expected download name in a Cucumber scenario, using Capybara or Webrat to check the Content-Disposition header.

Test the content-type of a response in Cucumber

Check a download or page response by its MIME type in Cucumber, using Webrat or Capybara; the Selenium webdriver does not support this approach.

Cucumber steps to test input fields for equality (with wildcard support)

Exact form-field matching in Cucumber prevents false positives from partial values, while wildcards allow flexible contains-style checks.

Default block arguments for Ruby 1.8.7

Ruby 1.8.6 and 1.8.7 do not support default block arguments; a lambda workaround uses *args and assigns missing values manually.

Git: See all unpushed commits or commits that are not in another branch

Local commits missing from a remote or upstream branch are easy to identify with git cherry -v, which lists the commits not shared by both branches.

Creating Liquid Layouts with Negative Margins

I was recently confronted with the task of creating a two-column liquid layout with a header and footer in which the content needed to come before the sidebar in the source code. I took opportunity to demonstrate an under-used aspect of CSS: negative margins. Negative margins allow us to push the content area away from the sides of the browser, leaving room for the sidebar.

Git: Define a custom merge driver

The ‘merge.*.driver` variable’s value is used to construct a command to run to merge ancestor’s version, current version and the other branches’ version. The merge driver is expected to leave the result of the merge in the file named with %A by overwriting it, and exit with zero status if it managed to merge them cleanly, or non-zero if there were conflicts.

Overriding unary operators in Ruby

This must be one of the coolest, yet quite unknown, technique in Ruby. For certain types of problems (e.g. when you have a set of rules) this gives you such an elegant way of describing the solution. There’s no meta programming or monkey patching involved, it’s short and sweet and best of all: it’s very intuitive.

Apache: Redirect all requests from one host to another

Redirecting all requests from one host to another preserves path and query string while avoiding duplicate hosts and broken links.

Word boundaries in MySQL regular expressions

MySQL lacks \b word boundaries in regular expressions, so word-start and word-end matching requires MySQL-specific markers instead.

Apache: Require username/password authentication except from a single IP, host or network

Apache Basic auth can stay enabled while a specific IP, host, or network bypasses login by combining access rules with Satisfy any.

Parametrized shared example groups in RSpec

Parameterized shared examples make reusable RSpec behavior checks accept arguments and blocks, avoiding duplicated expectations across similar specs.

Rails for Zombies

Learning Rails for the first time should be fun, and Rails for Zombies allows you to get your feet wet without having to worry about configuration. You'll watch five videos, each followed by exercises where you'll be programming Rails in your browser.

Export CSV from MySQL directly

If you need to export data from MySQL to a CSV, you can profit from really fast built-in methods.

This query writes the data to /tmp/geodb_coodinates.csv. And it's fast: Query OK, 337925 rows affected (0.86 sec)

SELECT * INTO OUTFILE '/tmp/geodb_coordinates.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' FROM geodb_coordinates;

Proper cross-browser CSS styling for buttons

Cross-browser button styling can vary with browser and OS defaults, especially for image-based designs. A small Sass reset removes inconsistent padding and borders in button elements.

Reset MySQL query cache

To clear the query cache in your MySQL database manually, e.g. for database profiling, execute the following command in your MySQL console:

RESET QUERY CACHE;

Git: Accessing lost commits

Amended, rebased, or reset commits are still reachable by SHA1 for a time; git reflog reveals recent branch positions before garbage collection removes them.

Apply a new callback to existing records

Run a newly added model callback for existing production records without replaying validations and unrelated callbacks; suitable for backfilling cached data after deployment.

Check if a field or button is disabled with Cucumber

Check whether form controls or buttons are disabled in Cucumber step definitions, using label-based assertions on Capybara or Webrat elements.

The Ruby Infinite Hash

How to create an infinitely nestable hash that always defaults to a new hash if a key does not map to a value.