Stubbing terminal user input in RSpec

Reading user input in console applications is usually done using Kernel#gets. Stubbing that can be a bit hairy.

When your code expects user input, you can not say Kernel.stub(gets: 'user input'). This will have no effect because of reasons.

Instead, you need to know which class will call gets. For example:

described_class.any_instance.stub(gets: 'user input')

If you do not know where gets is called, you can try something like this:

Object.any_instance.stub(gets: 'user input')

Any instance of an object should…

HAML 4+ expands nested element attributes

As you may know, HAML expands data attributes that are given as a hash:

%div{ data: { count: 3 } }
# results in:
<div data-count="3"></div>

However, this also works for any other hash attribute. Consider an Angular directive or an Unpoly compiler that is configured by several attributes. Usually you'd prefix them with the directive/compiler name so it gets clear where the attribute belongs. With HAML, this is easy to build:



PostgreSQL and its way of sorting strings

PostgreSQL uses the C library's locale facilities for sorting strings:

  • First, all the letters are compared, ignoring spaces and punctuation.
  • Then, spaces and punctuation are compared to break ties.
  • It sorts upper and lower case letters together. So the order will be something like a A b B c C


Ruby PostgreSQL
IMAGE3.jpg image2.jpg
image.jpg image3.jpg
image2.jpg IMAGE3.jpg
image3.jpg image.jpg

[PostgreSQL-FAQ: Why_do_my_strings_sort_incorrectly](…

External contentRepeats

Tasks, microtasks, queues and schedules -

The way that Javascript schedules timeouts and promise callbacks is more complicated than you think. This can be the reason why callbacks are not executed in the order that they are queued.

Please read this article!

External content

Thunderbird add-on to manually sort accounts and folders

This extension allows you to manually sort (order) your folders in the folder pane of Thunderbird or automatically sort them, but in a better way. This extension also allows you to re-order accounts in the folder pane.


Merge two arbitrary scopes in Rails 3

Rails 3 allows you to join two scopes from arbitrary sources by calling the merge method:

scope1 = User.where(:email => '')
scope2 = User.where(:first_name => 'hans')
merged_scope = scope1.merge(scope2)

merged_scope.all will now trigger a query for the combined scope chain:

SELECT `users`.* FROM `users` WHERE `users`.`email` = '' AND `users`.`first_name` = 'hans'

If you are joining two models, you can also [merge scopes for different models](…

Testing terminal output with RSpec

When testing Ruby code that prints something to the terminal, you can test that output.
Since RSpec 3.0 there is a very convenient way to do that.

Anything that writes to stdout (like puts or print) can be captured like this:

expect { something }.to output("hello\n").to_stdout

Testing stderr works in a similar fashion:

expect { something }.to output("something went wrogn\n").to_stderr

Hint: Use heredoc to test multi-line output.

expect { something }.to output(<<-MESSAGE.strip_heredoc).to_stdout...

About IE's Compatibility mode

IE has a "Compatibility Mode" for old browsers. You can keep IE from offering it (and fix some other things, too) by adding this meta tag to your <head>:

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

Or in Haml:

%meta(http-equiv="X-UA-Compatible" content="IE=Edge")

However, there are some things you need to bear in mind:

  • X-UA-Compatible is ignored unless it's present inside the first 4k of you page. If you put it somewhere in the bottom of your head section (or in the body) move it to top. The best place for …

instance_eval behaves different in Ruby 1.8 and Ruby 1.9, use instance_exec instead

In Ruby 1.9, instance_eval calls the block the with receiver as the first argument:

  • In Ruby 1.8, receiver.instance_eval(&block) calls
  • In Ruby 1.9, receiver.instance_eval(&block) calls

This will blow up in your face in Ruby 1.9, where a lambda crashes when it is called with a different number of arguments:

wrong number of arguments (1 for 0) (ArgumentError)

Forget that instance_eval ever existed. Use instance_exec instead, which behaves consistently across all Rubies.

factory_girl: Re-use partial factory definitions

Let's say you have two factories that share some attributes and traits:

FactoryGirl.define do

  factory :user do
    screen_name 'john'
    email ''
    trait :with_profile do
      age 18
      description 'lorem ipsum'
  factory :client do
    full_name 'John Doe'
    email ''
    trait :with_profile do
      age 18
      description 'lorem ipsum'

The attached patch let's you extract the shared behavior into a mixin:

FactoryGirl.define do

mixin :person do

External contentRepeats

Getting permanent links to files on Github or Gitlab

Please don't simply copy line number links from Github. The URL usually contains a branch name like master:

If someone now posts an insertion or deletion to that file into master your link points to the wrong line!

A better way is to (Github) press the Y key after clicking on a line number or (Gitlab) clicking the "Permalink" button in the top bar.

This will transform the URL to another URL that points to the particular commit:


Ruby: Converting UTF-8 codepoints to characters

Converting string characters to or from their integer value (7-bit ASCII value or UTF-8 codepoint) can be done in different ways in Ruby:

  • String#ord or String#unpack to get character values
  • Integer#chr or Array#pack to convert character values into Strings

Character values to Strings


To get the character for a 7-bit ASCII value or UTF-8 codepoint (0-127) you can use Integer#chr:

# => "t"

To get a character for values larger than 127, you need to pass the encoding. E.g. to get codepoint 25…

External contentAuto-destruct in 19 days

Unpoly 0.27.0 released


Compatible changes

  • Calling up.log.enable will now keep logging enabled for the remainder of this
    browser session (and persist through page reloads).
  • Added experimental events to observe history changes: up:history:push (preventable), up:history:pushed and up:history:restored
  • Fix a bug where prepending or appending multiple elements with :before / :after

How to test whether your device has a true Retina display

The linked site hosts a simple test. It shows two images with narrow vertical/horizontal lines. The more they resemble each other, the more Retina your display is.


ruby-debug: Find out current debugger position

So you are debugging like a boss and lost track of where you actually are in your code? No problem:

  • Calling "l=" will show you the current file and line. That's a lower-case L and an equals sign.
  • "where" (or "backtrace") will give you the debugger call stack, including current file and line as well. It can be quite long.

Rails Env Widget

Have you ever mistaken one Rails environment for another? The attached helper will help you to never do it again.

Save the attached file to app/helpers/ and use the widget in your layout like this:

  = rails_env_widget unless Rails.env.production?

It'll render a small light gray box in the top left corner of your screen, containing the current Rails environment. On click, it'll disappear. Actually, it has the same UX as our Query Diet widget.


Traverse an ActiveRecord relation along an association

The Edge Rider gem gives your relations a method #traverse_association which
returns a new relation by "pivoting" around a named association.

Say we have a Post model and each Post belongs to an author:

class Post < ActiveRecord::Base
  belongs_to :author

To turn a relation of posts into a relation of its authors:

posts = Post.where(:archived => false)
authors = posts.traverse_association(:author)

You can traverse multiple associations in a single call.
E.g. t…

Lazysizes 2 is here

It claims to be even faster and brings a new plugin that polyfills object-fit and object-position. This allows for easy arrangement of e.g. images and videos inside containers.

As puts it, object-fit/object-position is a:

Method of specifying how an object (image or video) should fit inside its box. object-fit options include "contain" (fit according to aspect ratio), "fill" (stretches object to fill) and "cover" (overflows box but maintains ratio), where object-position allows the object to be repositioned like backg…

3196 cards