There is no real performance difference between "def" and "define_method"

You can define methods using def or define_method. In the real world, there is no performance difference.

define_method is most often used in metaprogramming, like so:

define_method :"#{attribute_name}_for_realsies?" do

Methods defined via define_method are usually believed to have worse performance than those defined via def.
Hence, developers sometimes prefer using class_eval to define methods using def, like this:

class_eval "def #{attribute_name}_for_realsies?; do_things; end"

You can be…

Images darken when getting converted with ImageMagick

When using ImageMagick to manipulate images, you might see that images get darker when beeing modified by newer versions of ImageMagick.

This usually happens with CMYK images beeing converted to RGB by IM.


At least in our version of ImageMagick (6.7.7) you can solve this by passing this parameter to ImageMagick -colorspace sRGB.
The RGB colorspace was okay for ImageMagick until version 6.6.9. RGB and sRGB switched obviously.

has_one association may silently drop associated record when it is invalid – Rails bug?

This is quite an edge case, and appears like a bug in Rails (4.2.6) to me.


# post.rb
class Post < ActiveRecord::Base
  has_one :attachment
# attachment.rb
class Attachment < ActiveRecord::Base
  belongs_to :post
  validates :title, presence: true


When creating a post with an invalid attachment, I expect the creation to fail because of attachment validation errors. However, the attachment is silently not stored to the database.

post = Post.create! attachment: #…


Git: How to rebase your feature branch from one branch to another

In a nutshell: Use git rebase --onto target-branch source-branch

  • target-branch means "branch you want to be based on"
  • source-branch means "commit before your first feature commit"

Let's say my-feature-branch is based on master and we want it to be based on production. Consider this history (topmost = latest):

  • commit 6 [my-feature-branch]
  • commit 5
  • commit 4 [master]
  • commit 3
  • commit 2 [production]
  • commit 1

Here, master has commits that are not yet in production (number 3 and 4).


Ruby 2.3 new features

Ruby 2.3.0 has been around since end of 2015. It brings some pretty nice new features! Make sure to read the linked post with its many examples!

Similar to Hash#fetch, but for multiple values. Raises KeyError when a key is missing.
attrs = User.last.attributes
attrs.fetch_values :name, :email
Turns a Hash into a Proc that returns the corresponding value when called with a key. May be useful with enumerators like #map:
attrs.keys.grep(/name/).map &a...

Linux: Find out which processes are swapped out

Processes in Linux might be put into Swap ("virtual memory") occasionally.
Even parts of a single process might be removed from memory and put into Swap.

In order to find out which processes remain within Swap, run this:

sudo grep VmSwap /proc/*/status | egrep -v "0 kB"

Keep in mind Swap is not evil by definition. Some bytes per process beeing put to Swap will not have that much of performance influence.

If you want the Linux virtual memory manager (which is responsible for the decision if and which processes are moved to Swap) to be…


You can implement basic object-fit behavior with background images

So you want to use object-fit, but you also need to support Internet Explorer.

One option is to use lazysizes as a kinda-polyfill. Another option is to implement the requirement with background-size: contain, and background-size: cover, which is supported in IE9+.

E.g. to make an image cover a 100x100 px² area, cropping the image when nece…


Icon font vertical alignment in Windows

I had an issue with icons from an icon font aligning differently on Linux, iOS and Windows (seemingly browser-independent). With vertical-align:middle, they aligned properly on Linux, iOS and macOS, whereas with a vertical-align of -18%, it looked good on Windows and iOS, but not Linux.

Further investigation showed that not only icons, but also normal capital letters aligned differently. No setting of vertical-align could fix this, neither top, bottom, middle, nor additional paddings or margins. It seems like browsers take the…

ActiveRecord autosave

When ActiveRecord child objects are autosaved in Rails.


Don't compare datetimes with date ranges in MySQL

When selecting records in a date range, take care not to do it like this:

start_date = Date.parse('2007-05-01')
end_date = Date.parse('2007-05-31')
LogItem.where(:created_at => start_date .. end_date)

The problem is that created_at is a datetime (or Time in Ruby), while start_date and end_date are simple dates. In order to make sense of your query, MySQL will cast your dates to datetimes where the time component is 00:00:00. Because of this the query above will lose records created from 2007-05-31 00:00:01 t…

Download Ruby gems without installing

You can download .gem files using gem fetch:

gem fetch activesupport consul

This will produce files like active-support-5.0.0.gem and consul-0.12.1.gem in your working directory.

Dependencies will not be downloaded.

virsh reload xml config file

If you have made any changes to a libvirt xml config file you have to reload is. One way is to reload the whole libvirtd service. But you can just reload corresponding domain xml too:

$ virsh define foo.xml
External content

ActiveRecord subselects - Today I Learned

Apparently you can pash a second scope to a hash-condition and the whole thing will be evaluated as a second SELECT statement with a subselect.

Note that sub-queries are extremely slow in MySQL, but they can make cases easier where performance does not matter so much (e.g. a migration on 50K records).

External content

The Codeless Code

The Codeless Code is a charming series of stories about monks and nuns at a programming monastery.

Some stories to get started:

External contentRepeats

Verifying doubles in RSpec 3

RSpec 3 has verifying doubles. This breed of mock objects check that any methods being stubbed are present on an instance of a given class. They also check methods aren't called with the wrong number of arguments.

This dual approach allows you to move very quickly and test components in isolation, while
giving you confidence that your doubles are not a complete fiction.

You should always prefer using a verifying double to using an old-…

Ruby's default encodings can be unexpected

Note: This applies to plain Ruby scripts, Rails does not have this issue.

When you work with Ruby strings, those strings will get some default encoding, depending on how they are created. Most strings get the encoding Encoding.default_internal or UTF-8, if no encoding is set. This is the default and just fine.

However, some strings will instead get Encoding.default_external, notably

  • the string inside a
  • some strings created via CSV
  • files read from disk
  • strings read from an IRB

Encoding.default_external d…

Ruby 2.3.0 has a safe navigation operator

As announced before, Ruby has introduced a safe navigation operator with version 2.3.0. receiver&.method prevents NoMethodErrors by intercepting method invocations on nil.

user = User.last
user&.name # => "Dominik"
# When there is no user, i.e. user is nil:
user&.name # => nil

This might remind you of andand, and indeed it behaves very similar. The only difference is in handling of `fa…

Ruby 2.3 brings Array#dig and Hash#dig

#dig lets you easily traverse nested hashes, arrays, or even a mix of them. It returns nil if any intermediate value is missing.

x = {
  foo: {
    bar: [ 'a', { baz: 'x' } ]

x.dig(:foo, :bar) # => [ 'a', { baz: 'x' } ]
x.dig(:foo, :bar, 1, :baz) # => "x"
x.dig(:foo, :wronk, 1, :baz) # => nil

There is a tiny gem that backports this.

3209 cards