Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Created April 3, 2012 18:31
Show Gist options
  • Save pmarreck/2294461 to your computer and use it in GitHub Desktop.
Save pmarreck/2294461 to your computer and use it in GitHub Desktop.
How to get hash format flipping between Ruby 1.8-style and Ruby 1.9-style in Sublime Text 2
# First, install the Sublime package SublimeExternalCommand with the Package Control sublime package.
# Then add the following file in your ~/bin (or somewhere in your PATH, note that you may have to change some paths later here) and call it "rubyhashflip" and chmod +x it:
#!/usr/bin/env ruby
begin
require 'hash_syntax'
rescue LoadError
puts `gem install hash_syntax 2>&1`
require 'hash_syntax'
end
include HashSyntax::Transformer
input = ARGF.readlines.join # this should take stdin OR a filename on params list
# determine which direction you want to go by matching on old rocket in input
direction_19 = input.match /\=>/
print transform(input,{:"to-1#{direction_19 ? '9' : '8'}" => true})
# Then add the following to Sublime Text 2 > Preferences > Key Bindings - User, replacing YOURUSERNAME with you. If you don't have any bindings yet you have to wrap the whole thing in an array [] :
{ "keys": ["ctrl+super+h"], "command": "filter_through_command", "args": { "cmdline": "/Users/YOURUSERNAME/.rvm/bin/rvm-auto-ruby /Users/YOURUSERNAME/bin/rubyhashflip" } }
# Now, control-command-H should toggle your selected hash syntax OR the whole file's if nothing is selected.
# You can use this in conjunction with the Alignment package to get aligns with ruby 1.9 hashes to work properly! (just convert them to 1.9 after alignment)
# BONUS! Get Sublime to use your rvm ruby... Change your ~/Library/Application Support/Sublime Text 2/Packages/Ruby/Ruby.sublime-build to this, replacing YOURUSERNAME:
{
"cmd": ["/Users/YOURUSERNAME/.rvm/bin/rvm-auto-ruby", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.ruby"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment