Ruby 1.9 introduced a new json style ruby hash syntax. Although we can use this to define static hash objects and puts a limitation on the key we can use, it’s a fairly neat syntax nevertheless. However I work on several projects, both new and old and hence the new syntax is not always readily available.
1 2 3 4 5 | # Old style hash hash = { :a => 'test' } # New style hash hash = { a: 'test' } |
To demonstrate the sheer power of regular expressions & VIM, here’s a tiny snippet that allows you to quickly toggle a ruby hash syntax from one form to other.
1 2 3 4 5 6 7 8 9 | function! s:RubyHashSyntaxToggle() range if join(getline(a:firstline, a:lastline)) =~# '=>' silent! execute a:firstline . ',' . a:lastline . 's/[^{,]*[{,]\?\zs:\([^: ]\+\)\s*=>/\1:/g' else silent! execute a:firstline . ',' . a:lastline . 's/[^{,]*[{,]\?\zs\([^: ]\+\):/:\1 =>/g' endif endfunction command! -bar -range RubyHashSyntaxToggle <line1>,<line2>call s:RubyHashSyntaxToggle() noremap <Leader>rh :RubyHashSyntaxToggle<CR> |