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.

MySQL: Select a default value for NULL fields

NULL values break database calculations because arithmetic propagates NULL; IFNULL substitutes a fallback value so expressions continue to evaluate.

Prototype 1.7 is out

jQuery's selector engine, live()-like event handlers, pixel-perfect layout measuring.

Virtual attributes for array fields

Store associated values as a single array-like field, editable in forms and restored safely after validation errors, while syncing to the underlying relation.

Install RubyMine under Ubuntu

RubyMine on Ubuntu can be installed as a snap with automatic updates or set up manually from the legacy archive. Configuration stays in ~/.config/JetBrains/RubyMine<version> with classic confinement.

Installing Nokogiri

Because Nokogiri needs to be compiled and dynamically linked against both libxml2 and libxslt, it has gained a reputation for being complicated to install. Let’s wrassle this little myth to the ground, shall we?