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

RSpec 1, RSpec 2

To test whether two arrays have the same elements regardless of order, RSpec 1 and 2 give you the =~ matcher:

actual_array.should =~ expected_array

Rspec 3

With RSpec 3's expect syntax you can choose one of these two matchers:

expect(actual_array).to match_array(['1', '2', '3'])
expect(actual_array).to contain_exactly('1', '2', '3')

Note how match_array takes an argument, but contain_exactly takes a list of elements as varargs.

Test::Unit

If y...

Encode or decode HTML entities

Use the htmlentities gem.

Encoding works like this:

require 'htmlentities'
coder = HTMLEntities.new
string = "<élan>"
coder.encode(string)               # => "&lt;élan&gt;"
coder.encode(string, :named)       # => "&lt;&eacute;lan&gt;"
coder.encode(string, :decimal)     # => "&#60;&#233;lan&#62;"
coder.encode(string, :hexadecimal) # => "&#x3c;&#xe9;lan&#x3e;"

Decoding works like this:

require 'htmlentities'
coder = HTMLEntities.new
string = "&eacute;lan"
cod...

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

If you modified git's history and the change was already pushed, you will usually get a
! [rejected] my-branch -> my-branch (non-fast-forward)
error, when trying to push.

You can force the push, with
git push --force origin my-branch

Careful:

  • You might lose history.
  • Unless your git is configured to push only the current branch, you must supply the remote branch name or you will force-push all your branches!
  • Anyone else who has already pulled the changes will run into significant trouble.

Only...

Git: Changing commit messages

To change the commit message of the latest (unpushed, unmerged) commit, you can use
git commit --amend

To change the commit message of an earlier (unpushed, unmerged) commit [COMMIT], you can do
git rebase -i COMMIT~

For a current version of git, you can simply mark the line with "reword", and git will ask you later for the new message.

For older versions:

  • mark the line with edit
  • save the file
  • do a git commit --amend when rebasing stops at the relevant commit
  • git rebase --continue

Git: Amending older commits

Lets say you need to make a change to a commit OLD_COMMIT, but this is not the most recent. If you have neither pushed nor merged it, you can do this:

  • Make a new commit now, with a message like "fix".
  • Do a
git rebase -i OLD_COMMIT~
  • In the editor window that opened, move the "fix" commit directly after the one you want to amend (so it should be the second from the top), and mark it as "fixup". Save the file.
  • If there are conflicts, solve them, add them, and do
git rebase --continue

Never use YAML.load with user input

You can get YAML.load to instantiate any Ruby object by embedding the desired class name into the YAML code. E.g. the following will create a new User object and set @email and @password to the given values:

--- !ruby/object:User
email: me@somewhere.com
password: secret

Considering the security implications, you should never trust YAML from untrusted sources. If you are looking for a simple, secure and readable data transfer format, use Object#to_json and JSON.parse.

Another way around YAML.load is [`YAML.safe_...

How to fix failing controller specs 91% of the time

If your controller spec never reaches your controller code:

  1. Make sure you are signed in.

  2. Make sure you are actually triggering a request by saying get :edit or something siliar.

  3. Know that views are not rendered by default for controller specs unless you tell them to (render_views).
    ^
    describe UsersController do
    describe '#edit' do
    it 'should work' do
    sign_in
    get :edit
    end
    end
    end

    define something like this in your spec_helper.rb:

    def sign_in(user = User....

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

To temporarily change the current working directory in Ruby, call Dir.chdir with a block. The previous working directory will be restored when the block ends:

Dir.chdir('/usr/local') do
  # do stuff in /usr/local
end

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

If you need to do calculations inside the database and can not use Ruby objects you may run into problems when encountering fields with NULL values:

SELECT foo, bar, foo - bar AS baz FROM plop;
+-----+------+------+
| foo | bar  | baz  |
+-----+------+------+
|  30 |   20 |   10 |
|  30 | NULL | NULL |
+-----+------+------+

Solve this by using IFNULL: it returns the selected value if present and a given alternative if it would select NULL:

SELECT foo, bar, foo - IFNULL(bar, 0) AS baz FROM plop;
+-...

Prototype 1.7 is out

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

Virtual attributes for array fields

When a has_many association basically serves to store a list of associated strings (tags, categories, ...), it can be convenient to represent this association as a string array in the containing model. Here is an example for this pattern from the acts-as-taggable-on gem:

post = Post.last
p post.tag_list # ['foo', 'bar', 'baz']
post.tag_list = ['bam']
p post.tag_list # ['bam']

This string array tag_list is magical in several ways:

  • It is read from and written to a `has...

Install RubyMine under Ubuntu

This card explains how to install RubyMine for the first time. If you want to upgrade an existing RubyMine installation (after legacy install) to a newer version, see How to upgrade RubyMine.


Option A (new way)

Ubuntu 16.04 comes with snap, a way to package software with all its dependencies. RubyMine is also packaged as a snap.

A snap will always track a channel (like stable, beta) and automatically update to the newest version available in this channel. By default the snap daemon will check for ...

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?

Counters for Partials

When rendering a partial with the :collection option, you are automatically provided with a counter variable inside the partial template.

Hunt down that elusive debug message in Ruby

When you just went through a long debug-fest and infested your code with dozens of debug messages, it can be hard to find all those calls to puts and p. This note describes a hack that lets you trace those messages in your code.

Let's say you want to get rid of a console message "foobar". Copy the Undebug class below to config/initializers.rb. In the same initializer, type a line:

Undebug.trace_message('foobar')

Now run tests or whatever you need to do to to trigger that message. The console output should look like this:

...

Disable automatic e-mail checking in Thunderbird 3

Have you guys ever done the math on that? You asked or allowed for 24000 interruptions from literally every human being in the world who could fall onto a keyboard and make an e-mail go to you. (Merlin Mann)

So you decided to put a price on your attention and not check your e-mail 24000 times a year. A first step is to disable automatic e-mail checks in Thunderbird.

  1. Open Edit -> Account settings and select your incoming mail account.
    ...

Take care when joining and selecting on scopes

Occasionally some complex query must be processed on the database because building thousands of Ruby objects is impracticable.

Many times you would use scope options, like this:

users = User.scoped(
  :joins => 'INNER JOIN orders joined_orders ON users.id = joined_orders.user_id',
  :conditions => [ 'joined_orders.date BETWEEN ? AND ?', start_date, end_date ],
  :select => '*, SUM(joined_orders.amount) AS amount_sum',
  :group => 'users.id'
)

You get ActiveRecord objects and you can ask each of them about its `amou...

Manipulate an array attribute using multiple check boxes

E.g. when you're using a tagging gem, you have seen virtual attributes that get and set a string array:

post = Post.last
puts post.tag_list # ['foo', 'bar', 'baz']
post.tag_list = ['bam']
puts post.tag_list # ['bam']

If you would like to create a form displaying one check box per tag, you can do this:

- form_for @post do |form|
  = form.check_box :tag_list, { :multiple => true }, 'foo', nil
  = form.check_box :tag_list, { :multiple => true }, 'bar', nil
  =...

Use the back button in Cucumber

In order to go back one page in your Cucumber tests, you can use the following step definition for Capybara:

When(/^I go back$/) do
  visit page.driver.request.env['HTTP_REFERER']
end

If you're on Webrat, this should work:

When(/^I go back$/) do
  visit request.env["HTTP_REFERER"])
end

An improved version of this step is now part of our gem spreewald on Github.