Search and replace with regex in Sublime Text

One of the things that we do a lot in our Ruby on Rails project is replace old-style Ruby hashes (“hash rockets”) with new style, more compact hashes.

So this

{ :key => "value" }

becomes this

{ key: "value" }

In Sublime Text, you can use a regex in find and replace – like this:

replace-rockets

Select the “regex” option (far left), and use the “…” button (middle right) to select “Add Current File” as the scope. Then use the “Replace” button (bottom left) – the prompt will tell you how many instances are going to be replaced.

Of course, if you’re feeling brave, you could do the same find and replace across ALL your files..

There’s some more about the new style here : http://blog.pluralsight.com/rip-ruby-hash-rocket-syntax

Two-Legged OAuth with the Google Drive API in Ruby

Google are discontinuing support for the Documents List API, and moving to the Drive API.

The old API supported authentication with a username and password, but that’s not allowed in the new API. Instead, you need to use OAuth for access.

If you want to have server-to-server authentication, without user interaction, you need a “two-legged” OAuth process, where a token is obtained with an encrypted request and then used for future service requests.

Continue reading Two-Legged OAuth with the Google Drive API in Ruby

from_sentence : the opposite of Rails to_sentence

I like the Rails to_sentence method on String class, which converts an array to a comma-separated sentence where the last element is joined by a connector word.

It makes it easy to take a list of names, for example, and make them human-readable, without having to fiddle with join words and last-item-is-special-case stuff:

['one', 'two'].to_sentence          # => "one and two"
['one', 'two', 'three'].to_sentence # => "one, two, and three"

Continue reading from_sentence : the opposite of Rails to_sentence

Sublime Text 3 : keyboard shortcut to reveal file in sidebar

To see where your current file is in the folder sidebar, you can install SyncedSideBar.

However, that has some side effects – sometimes, even if you’ve hidden the sidebar, it pops back up when you switch between files, or search.

Continue reading Sublime Text 3 : keyboard shortcut to reveal file in sidebar