View
External content

Pexels: Free stock photos

You can find great stock photos on pexels.com.

All pictures are free for personal and commercial use without attribution, and may be modified.

Repeats

Test if two date ranges overlap in Ruby or Rails

A check if two date or time ranges A and B overlap needs to cover a lot of cases:

  • A partially overlaps B
  • A surrounds B
  • B surrounds A
  • A occurs entirely after B
  • B occurs entirely after A

One trick to cover all these cases with a single expression is to see if the start date of each range is looking at the end date of the other range in the same direction.

The code below shows how to implement this in Ruby on Rails. The example is a class Interval, which has two attributes #start_date and #end_date. These dates are considere…

Auto-destruct in 57 days

Updated: Configure RSpec to raise an error when stubbing a non-existing method

  • Other than claimed previously, this is not the default in RSpec 3.3+. You need to enable it manually.
  • Fixed code sample that showed how to enable partial double verification.
External content

makandra/gemika: Helpers for testing Ruby gems

We have released a new library Gemika to help test a gem against multiple versions of Ruby, gem dependencies and database types.

Here's what Gemika can give your test's development setup (all features are opt-in):

  • Test one codebase against multiple sets of gem dependency sets (e.g. Rails 4.2, Rails 5.0).
  • Test one codebase against multiple Ruby versions (e.g. Ruby 2.1.8, Ruby 2.3.1).
  • Test one codebase against multiple database types (currently MySQL or PostgreSQL).
  • Compute a matrix of all possib…
Auto-destruct in 57 days

Updated: Execution of shell code in Ruby scripts

Added details about the two distinct argument patterns of system and exec.

Spoiler:

# Works
system 'bundle exec rails server'
system 'bundle', 'exec', 'rails', 'server'

# Fails
system 'bundle exec', 'rails server'
system 'bundle', 'exec rails server'
External content

What every coder should know about gamma

Good article about what "gamma correction" means for color processing, and what "sRGB" actually means.

You probably do not need to know this for web development, but it's an interesting read.

Repeats

Ruby tempfiles

Tempfiles get deleted automatically

With the the ruby Tempfile class you can create temporary files. Those files only stick around as long as you have a reference to those. If no more variable points to them, the GC may finalize the object at some point and the file will be removed from the filesystem. If you would try to access your tempfile then using its path (which you stored previously), you would get an error because the file no longer exists.

Unlink your tempfiles when you're done with them
-…

Detecting if a Ruby gem is loaded

Detect if a gem has been activated

A gem is activated if it is either in the current bundle (Gemfile.lock), or if you have manually activated it using Kernel#gem (old-school).

To detect if e.g. activerecord has been activated:

if Gem.loaded_specs.has_key?('activerecord')
  # ActiveRecord was activated
end

Detect if a particular gem version has been activated

To detect if e.g. activerecord matche…

Ubuntu Mate: Get rid of F12 drop-down terminal

To get your F12 key back for other shortcuts:

killall tilda
sudo apt-get remove tilda

Fix "libmysqlclient.so.20: cannot open shared object file: No such file or directory"

This error can be caused by the mysql2 gem under mysterious circumstances. You need to remove it with gem uninstall mysql2 and then reinstall it (or just run bundle).

gem pristine mysql2 will not be enough.

VCR fails if the same request is triggered multiple times

Same requests are recorded only once in vcr. Replaying a test fails, if you trigger the same request multiple times. The error message is somehow confusing, as your cassette contains the request:

An HTTP request has been made that VCR does not know how to handle

If you want to allow to match a request multiple times, you need to configure this explicit with allow_playback_repeats: true. Some example configurations:

```
# specific ca…

External content

Migrations are versioned in Rails 5 | BigBinary Blog

Rails 5 migration classes look like this now:

class CreateUsers < ActiveRecord::Migration[5.0]

Mind the [5.0] at the end.

They do this to evolve the ActiveRecord::Migration API without breaking your historical migrations in db/migrate.

Repeats

Ruby regular expression start/end line vs. start/end string

tl;dr: Most often you want to use \A and \z. Take care of using ^ and $!

^ Start of line
$ End of line

!!("image/jpeg" =~ /^image\/(jpeg|png)$/) => true
!!("image/png" =~ /^image\/(jpeg|png)$/) => true
!!("application/javascript" =~ /^image\/(jpeg|png)$/) => false

!!("application/javascript\nimage/jpeg\nfoo" =~ /^image\/(jpeg|png)$/) => true

\A Start of string
\z End of string

```
!!("image/jpeg" =~ /\Aimage\/(jpeg|png)\z/) => true
!!("image/png" =~ /\Aimage\/(jpeg|png)\z/) => true
!!("application/javascript" =~ /\Aimage\/(…

Auto-destruct in 59 days

Your stubs are full of lies: Remember to upgrade to RSpec 3.3+

This is a reminder that you can easily upgrade your Rails 3+ project to RSpec 3.3+.

You can configure RSpec 3.3+ to raise an error when stubbing a non-existing method (disabled by default). This might reveal some bugs in your tests for little to no cost.

You can enable this behavior by adding the following to your spec_helper.rb:

RSpec.configure do |config|
  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end
end
Auto-destruct in 54 days

Updated: How to use Javascript promises

  • Show how to construct a promise in jQuery
  • Show how to construct a native browser promise (available in modern browsers and Edge, but not IE)
  • Added link to curriculum lesson to help you practice

Sass: How to convert an RGBA color to its RGB look-alike

Say you have an RGBA color that you need as a non-transparent color because of reasons.

Basically, this is possible. Just understand that you will convert your RGBA color for exactly one base background color as you are giving up transparency.
Most likely, your background is white, so you'll use #fff as that base for examples below.

Simple approach

When your know the RGBA color's base RGB color (e.g. your brand color that you RGBA'd for some hover effect), you can simply use the mix function instead of rgba.

Before:

back...

Heads up: "localhost" may be IPv6 on new linuxes

I've encountered a Ubuntu 16.04 today, where localhost resolved to ::1 instead of 127.0.0.1.

This will not usually make a difference, but could be relevant for firewall policies.

List of handy Chrome plugins

These are Chrome plugins that proved useful at makandra. Each is the best-in-class.

Full Page Screen Capture
A webpage screenshot maker.
Page Ruler
Measure webpage pixels.
Accessibility Developer Tools
Get accessibility feedback on elements …
View
3198 cards