ruby

Allow pasting multiline ruby blocks in Pry when using leading dot notation

We use leading dot notation a lot now e.g.

Foo
  .bar
  .baz

If you copy and paste this into a Pry REPL it throws an error because it doesn't know how to parse it. I thought this was because Pry was parsing differently to IRB but it's actually because Pry has a feature to call system methods by prefixing them with a dot. To be able to copy paste code like this you can simply remove the Pry commands 

Pry.commands.delete /\.(.*)/
5
Joe

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
SONG

3
Const

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.

1
Babar

Automagical Intermediate-level Hashtables in Ruby

All intermediate-level hashtables will be created automagically

def autovivifying_hash
   Hash.new {|ht,k| ht[k] = autovivifying_hash}
end


Usage

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


Ali

Change VS Code Go to definition to Cmd+Click

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

    "editor.multiCursorModifier": "cmd",


6
Ali

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.

1
Const

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 { 'https://placehold.it/200x200' }

    initialize_with { new(image_url: image_url) }
  end
end
build(:citizen_id_image)
#=> 


5
Joe

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 }
827
Alyson

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*


828
Joe

Suppress rspec warnings

$ RUBYOPT=W0 ./bin/rspec spec/
...

$ RUBYOPT=W0 bundle exec rspec spec/
...

$ RUBYOPT=W0 rspec
...

$ RUBYOPT=W0 ...
...


827
Ali

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


2
Ali

Run rubocop before commit

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

#!/usr/bin/env ruby

require 'english'
require 'rubocop'

ADDED_OR_MODIFIED = /A|AM|^M/.freeze

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"


832
Ali