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]


Remove unwanted commit from your branch

git rebase -p --onto SHA^ SHA


Suppress rspec warnings

$ RUBYOPT=W0 ./bin/rspec spec/

$ RUBYOPT=W0 bundle exec rspec spec/

$ RUBYOPT=W0 rspec

$ RUBYOPT=W0 ...


Tag rails logs with useful information

Just learned about tagged logging in rails. Did you know you can tag a log by using

logger.tagged('Your Tag') { logger.info('Message') }

You can also do it globally with

# config/application.rb
config.log_tags = [:method_on_request_object, lambda { |request| request.method.modifier_method }]

Add client Device OS and OS Version information to logs (requires clients to send X-OS and X-OS-Version headers)

# config/application.rb
config.log_tags = [ :request_id, lambda { |request| "#{request.headers['HTTP_X_OS']} #{request.headers['HTTP_X_OS_VERSION']}" } ]


Limited has_many associations

# app/models/user.rb
has_one :four_car_garage
has_many :cars
validates :cars, length: { maximum: 4 }


How to really!! quit vim

In terminal

vim todo.txt

Colon (:) (lowercase) q vim :q

Just don't use MacVim @joe :D


How to quit Vim

  1. Install latest version of Vim
  2. Open a file in Vim
  3. ⌘-Q and close the terminal
  4. Reopen terminal

This is obviously a joke, it's impossible to quit Vim.


Create idempotent migrations

When you want to write destructive migrations please use if_exists: true when you’re trying to remove a table and check for a table existence when you want to add or remove a column ActiveRecord::Base.connection.table_exists?

Case 1:

def up
  drop_table :kittens, if_exists: true

Case 2:

def up
  return unless ActiveRecord::Base.connection.table_exists?(:kittens)
  add_column :kittens, :kind, :integer


Add spacers to your macOS app dock to make groups

Open term (or equivalent) and paste the following one-liner:

defaults write com.apple.dock persistent-apps -array-add '{tile-data={}; tile-type="spacer-tile";}'; killall Dock

When the Dock restarts, there should be a draggable blank space that you can move anywhere. Repeat to your heart's desire.


Send messages to Slack using cURL

Slack allows you to send messages via the Webhooks service

Create a webhook for any channel and then you can send notification using curl.

$ curl -X POST -H 'Content-type: application/json' --data '{"text": "Something important"}'  https://hooks.slack.com/services/YOUR/TOKENIZED/URL