Ruby 2.0 debugger

'Post-mortem' debugging (start debugger on exception)

require 'debug' # will drop into debugger console
catch BasicObject           # will make debugger stop on any exception
c               # will continue the program

Dropping into debugger again could be done by sending INT (C-c) or programmatically with DEBUGGER__.interrupt

SQL DECIMAL

DECIMAL(M,D)


 PRECISION (M)
/         \
1234567.123
        \ /
       SCALE (D)

M is the maximum number of digits (the precision). It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.)

D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

E.g.

DECIMAL(10,3) => 1234567.123

or in Rails speak

t.decimal :mycolumn, precision: 10, scale: 3 #=> 1234567.123

Confusingly number_to_percentage takes :precision option which...

Emacs Useful Bits

Indent

M-i indents current line, combined with multiple cursor - indents regions

Git

C-x v g Blame https://www.gnu.org/software/emacs/manual/html_node/emacs/Old-Revisions.html, use a to go back in history for current line

Nav

M-- M-d delete word backwards

C-x 4 c clone-indirect-buffer-other-window

Folding

M-1 C-x $ collapse everything indented

C-x $ show all

Search and replace

toggle-case-fold-search to replace without case gimmicks
M-x find-name-dired RET glob RET t Q to replace in m...

Printable BigDecimal

By default BigDecimal values are formatted in a not very user friendly way, to make BigDecimals look like float but still retain the distiction the following monkey-patch can be used

BigDecimal.class_eval do
  def inspect
    "#<#{self.class.name} #{to_f}>"
  end
end

Execute Rails migration code from IRB console

When in need to fix DB schema (e.g. replay bits of migration manually) or just run some arbitrary SQL from inside the IRB Rails console one has to prefix the methods like add_column or select_values with ActiveRecord::Base.connection, its easire to invoke new IRB session then with ActiveRecord::Base.connection passed as context, e.g.

irb(main):001:0> irb ActiveRecord::Base.connection
irb#1(#<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0xbc3fc994>):001:0> tables
=> ["users", ...]

The annoyingly long prompt can be...

Update Git repository URL for deployed Capistrano app

If repository URL has changed but the app shared copy still points to an old location the simplest way to update the URL would be to run shell and update the git remote origin like this

$ cap shell
cap> cd app/shared/cached-copy && git remote set-url origin 'git@bitbucket.org:mycorp/app.git'