Back up a file with same name + prefix

$ cp todo.txt !#:1.bak
cp todo.txt todo.txt.bak


NOTE: Actually you can use !#:1 in every command 'cause that's last argument passed to a command.

UPDATED: Now it works with zsh

239
Ali

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 }
1.07 Thousand
Alyson

How to find god objects in your project (Rails)

Go to your project root and cd into app/models (using CLI obviously) then run wc -lw * | sort -u

$ wc -lw * | sort -u
103     186 deal_broker.rb
116     313 physical_sim_report.rb
126     229 coupon_code.rb
126     260 subscription_mission.rb
129     284 payment.rb
145     290 deal_value.rb
147     255 single_use_coupon.rb
166     344 sim_card.rb
166     406 coupon_campaign.rb
225     451 order_summary.rb
260     534 deal.rb
308     783 package.rb
443    1031 user.rb
728    1744 subscription.rb
7262   15841 total


So now you can see Rocky is all about User ,Subscription and Package.

Note: First column number of lines and the second column number of words.

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*


1.07 Thousand
Joe

Chrome tab manipulation

Sometimes I want to go back to the last page I viewed while still keeping the same one open. You can very easily achieve this with shortcuts

  1. ⌘-L to focus the omnibox
  2. ⌘-Enter to open the current URL in a new tab
  3. ⌘-⇧-[ to go back to the original tab
  4. ⌘-[ to go back to the previously viewed page in the original tab
1.07 Thousand
Joe

Quickly move lines up and down in any Vim mode

I often move lines around in vim so I have created mappings to make this easier. These map to ⌘-j for move down and ⌘-k for move up. Works in normal, insert, and visual modes. vim nnoremap :m .+1== nnoremap :m .-2== inoremap :m .+1==gi inoremap :m .-2==gi vnoremap :m '>+1gv=gv vnoremap :m '<-2gv=gv I use MacVim so I have used D(⌘) for the modifier key. To use in terminal based vims, use a different modifier e.g.

1.11 Thousand
Joe

Delete already-merged branchs in Git

$ git branch --merged | egrep -v 'master|develop' | xargs -n 1 git branch -d


You can safelist more branches in egrep -v 'master|develop|foo|bar'

1.07 Thousand
Ali

Diff a local file with remote version in Git

Easily run the below command and get a diff version from the remote

$ git diff origin/master -- [local-path]


Local and Remote Branches


When using Git, it’s important to know the difference between local and remote branches. A local branch is a branch that only exists on your machine and is not shared with other developers or the remote repository. A remote branch is a branch that exists on the remote repository and can be accessed by other developers or through the remote repository.

Local branches are used for personal development, testing and experimentation, remote branches are used for collaboration, sharing and deployment. When you create a local branch you can work on it independently without affecting the remote branch. But when you’re ready to share your changes with others you can push your local branch to the remote repository, creating a new remote branch or updating an existing one.

Fetching the latest changes


Before comparing local and remote branches you need to fetch the latest changes from the remote repository. This will make sure your local repository is up to date with the latest changes from the remote repository. To fetch the latest changes use the git fetch command. This command updates your local repository with the latest changes from the remote repository without merging them.

Using Git Diff to compare files


Git Diff is a great tool to compare files between local and remote branches. To compare files using Git Diff use the git diff command followed by the names of the branches you want to compare. For example to compare the files in the local branch main with the files in the remote branch origin/main use the command git diff main origin/main.

Git Diff will show you the differences between the two branches, it will highlight the changes made in the remote branch compared to the local branch. You can also use the --stat option to show a condensed version of the differences, showing the number of added, deleted and modified lines.

Best practices for comparing local and remote


When comparing local and remote branches you should follow best practices to get accurate and fast results. Here are some best practices to keep in mind:

  • Always fetch the latest changes from the remote repository before comparing local and remote branches.
  • Use Git Diff to compare files between local and remote branches.
  • Use the --stat option to show a condensed version of the differences.
  • Use the git log command to compare commit histories between local and remote branches.
  • Use the git status command to see the status of your local branch and the remote branch.

Advanced comparison techniques

In addition to Git Diff and Git Log there are advanced comparison techniques you can use to compare local and remote branches. Here are a few:

  • Use git difftool to compare files between local and remote branches using a graphical difftool.
  • Use git diff --name-only to list the modified files between local and remote branches.
  • Use git diff --word-diff to show the differences word by word.
  • Use git diff --ignore-space-change to ignore whitespace changes.

Now you have more tools to compare local and remote branches.

1.07 Thousand
Ali

Remove unwanted commit from your branch

git rebase -p --onto SHA^ SHA


1.07 Thousand
Tino

Suppress rspec warnings

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

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

$ RUBYOPT=W0 rspec
...

$ RUBYOPT=W0 ...
...


RSpec warnings can clutter your test output and make logs harder to read, especially in large Rails projects. Here’s how to suppress them effectively while maintaining a clean workflow.

Suppress Warnings Globally


You can disable warnings for your entire test suite by adding the following to spec_helper.rb or rails_helper.rb:

$VERBOSE = nil
Use Case: Ideal for quickly silencing all warnings, but it may hide important information across your app and dependencies.

Suppress Specific Warnings


Wrap warning-generating code in a suppression block:

def suppress_warnings 
original_verbose = $VERBOSE 
$VERBOSE = nil 
yield 
ensure 
$VERBOSE = original_verbose 
end 

suppress_warnings do 
require 'some_gem_with_warnings' 
end

Use Case: Useful for isolating warnings to specific dependencies or blocks of code.

Suppress RSpec Warnings


RSpec provides built-in configuration for managing its own warnings. Add this to your spec_helper.rb:

RSpec.configure do |config| 
config.warnings = false 
end

Use Case: Suppresses RSpec-specific warnings while keeping Ruby and dependency warnings visible.

Redirect Deprecation Warnings


For deprecation warnings, redirect them to a log file for review:

RSpec.configure do |config| 
config.deprecation_stream = File.open('log/rspec_deprecations.log', 'w') 
end

Use Case: Keeps test output clean while preserving visibility into deprecations.

Environment-Specific Suppression


Suppress warnings only in specific environments, such as CI:

$VERBOSE = nil if ENV['SUPPRESS_WARNINGS'] == 'true'
Use Case: Allows warnings during local development but hides them in CI for cleaner logs.

By tailoring these approaches to your needs, you can keep your test outputs clean and actionable, improving productivity in development and CI workflows.
1.07 Thousand
Ali