Heredoc without interpolation

Sometimes you want to use Ruby's Heredoc without interpolation ('#{....}'). You can do this by adding single dashes around your keyword:
doc = <<-'SONG'
Business men, they drink my wine
#{person} dig my earth
None will level on the line
Nobody offered his word
Hey, hey


Change Ruby version for single command with rbenv

You can switch Ruby version temporarily with rbenv local

But it'd need to be switched back after you're done. Specially for a single command it's kind of pain. In those cases, you can use RBENV_VERSION env variable.

RBENV_VERSION=2.6.3 ruby -v

This might be useful in a shell script or so.


Automagical Intermediate-level Hashtables in Ruby

All intermediate-level hashtables will be created automagically

def autovivifying_hash {|ht,k| ht[k] = autovivifying_hash}


response_builder = autovivifying_hash
response_builder['payment']['status'] = 'OK'
response_builder # => {"payment"=>{"status"=>"OK"}


Change VS Code Go to definition to Cmd+Click

Just open your user settings JSON file and use this option:

    "editor.multiCursorModifier": "cmd",


Improve Bundler performance

Level up your bundler performance by running bundler tasks concurrently in jobs:

$ bundle config --global jobs 4

See more options on the bundler website.


You can use FactoryBot for non ActiveRecord models

You can use FactoryBot's initialize_with method to initialize an object any way you like which allows you to factorize any of your objects at will

FactoryBot.define do
  factory :citizen_id_image, class: CitizenIdImagePresenter do
    image_url { '' }

    initialize_with { new(image_url: image_url) }


optional relation + RSpec w/shoulda-matchers

User 1 - 0..1 Laptop

  • Model ```ruby a/m/user.rb has_one :laptop

a/m/laptop.rb belongs_to :user, optional: true ```

  • spec ruby s/m/laptop_spec.rb it { should belong_to(:user).optional }

Remove ruby 2.6 bundled bundler

Generating a rails 6 app with ruby 2.6+ when you have bundler v2 installed creates unusable binstubs. If you are seeing this error You must use Bundler 2 or greater with this lockfile. you can remove the bundler that comes bundled with ruby 2.6 and rely on the manually installed v2 bundler.

If you are using rbenv you can use this command (adjust for your ruby version e.g. 2.6.{0, 1} etc), other installation systems will use different locations

rm -rf ~/.rbenv/versions/2.6.1/lib/ruby/2.6.0/bundler*


Suppress rspec warnings

$ RUBYOPT=W0 ./bin/rspec spec/

$ RUBYOPT=W0 bundle exec rspec spec/

$ RUBYOPT=W0 rspec

$ RUBYOPT=W0 ...


Run rubocop against your current branch

$ git diff develop feature/one-signal --diff-filter=M --name-only -- *.rb | egrep -v 'db|config|spec' | xargs rubocop


Run rubocop before commit

Copy this into .git/hooks/pre-commit (remove .sample)

#!/usr/bin/env ruby

require 'english'
require 'rubocop'


changed_files = `git status --porcelain`.split(/\n/).
    select { |file_name_with_status|
      file_name_with_status =~ ADDED_OR_MODIFIED
    map { |file_name_with_status|
      file_name_with_status.split(' ')[1]
    select { |file_name|
      File.extname(file_name) == '.rb'
    }.join(' ')

system("rubocop #{changed_files}") unless changed_files.empty?

exit $CHILD_STATUS.exitstatus

Make it executable

$ chmod +x .git/hooks/pre-commit

You're good to go, from now it'd run rubocop against your changes and prevents from commiting unless you fix the offences.

How to skip pre-commit? just pass -n to git commit

$ git commit -n -m "[hotfix] blah blah"