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.

Force net/http to verify SSL certificates

Ruby's net/http is setup to never verify SSL certificates by default. Most ruby libraries do the same. That means that you're not verifying the identity of the server you're communicating with and are therefore exposed to man in the middle attacks. This gem monkey-patches net/http to force certificate verification and make turning it off impossible.

ActiveRecord Table Transform

How to write to the db 27,000 times in 24 seconds instead of 9 minutes.

Test that an exception or error page is raised in Capybara

Capybara error-page assertions rely on the response status code and require @allow-rescue; Selenium @javascript scenarios do not support page.status_code.

Ruby: Checking if a class is a descendant of another class

Ruby classes can be tested for direct superclass, any ancestor, or instance compatibility with superclass, ancestors, and is_a?.

Check if two arrays contain the same elements in Ruby, RSpec or Test::Unit

Array equality without regard to order is awkward in Ruby tests; RSpec and Test::Unit provide matchers for matching elements, and plain Ruby needs custom comparison logic.

Encode or decode HTML entities

Convert accented and reserved characters to HTML entity references, or turn entity strings back into readable Unicode text, with the htmlentities gem.

Capistrano cowboy deploys

Sometimes, you just need to shoot from the hip…or deploy your local changes without committing them. Put this snippet from Jesse Newland in ~/.caprc and now you can cap cowboy deploy.

Use the contents of a WordPress database in your Rails app

These two models can be used to access the posts and associated comments of a WordPress database.

Git: Force a rejected push

Rejected non-fast-forward pushes happen after rewriting shared history; git push --force overwrites the remote branch and can make collaborators lose commits.

Git: Changing commit messages

Change a Git commit message before it is pushed or merged. git commit --amend updates the latest commit; interactive rebase can rewrite earlier ones.

Git: Amending older commits

Older unpushed commits can be amended by folding a later fix commit into them with interactive rebase and fixup, preserving history without a new visible correction commit.

Never use YAML.load with user input

Untrusted YAML can instantiate arbitrary Ruby objects and set their fields, creating a serious deserialization risk. JSON.parse or YAML.safe_load are safer alternatives.

How to fix failing controller specs 91% of the time

Controller specs can fail without reaching controller code when authentication is missing, no request is triggered, or views stay unrendered.

Open a page in the default browser

Use the Launchy gem:

Launchy.open('http://www.ruby-lang.org/')

The LaTeX Font Catalogue

Gallery of fonts you can use without much hassle in LaTeX. The license of the fonts vary, but are all free. Note that the fonts not necessarily are free to distribute, and some fonts are available for non-commercial use only.

Change the current directory without side effects in Ruby

Temporarily switch directories in Ruby without leaving the process in a different working folder by using Dir.chdir with a block.

Ruby 2.0 Refinements in Practice

The first thing you need to understand is that the purpose of refinements in Ruby 2.0 is to make monkey-patching safer. Specifically, the goal is to make it possible to extend core classes, but to limit the effect of those extensions to a particular area of code. Since the purpose of this feature is make monkey-patching safer, let’s take a look at a dangerous case of monkey-patching and see how this new feature would improve the situation.