Clean, Simple and Updated: Three Awesome Regexes For Rails Developers [Tip]

Tip #1

Rails previously used a symbol and a hashrocket to declare/modify hashes, like so:

:name => "Mr. Foo B. Baz"

Now it uses this JSON-inpired style:

name: "Mr. Foo B. Baz"

Regex in ST2

In order to move to the pretty, concise new style (heh, I like it!) go a regex-replace in your text editor and replace this:

:(\w+) =>

with this:


This is what happens:

Screenshot from 2013-05-04 15:14:00 Screenshot from 2013-05-04 15:14:13

Tip #2

When you use something like Rails Composer(which is an awesome tool, BTW) you get a messy Gemfile like this:

A messy gemfile

You can fix some of that with the first tip, but for neat grouping, do this:

  1. In your editor’s find/replace box, enter this regex:
    gem .* (\w+): :development(?:.*)
  2. Select and cut all the lines.
  3. Create a new “group :development” block and paste the cut lines in.
  4. Repeat this with “group :test”, “group :production”, “group :staging”(if you have one) and “group :development, :test”. (For the last one, your regex should have “[:development, :test]”)

Note to self: write an awesome gem to cleanup Gemfiles :idea:

Tip #3

Jackie-Chan-WTFDo you have hardcoded version numbers in your Gemfile?

Select all text matching this regex:

, [\"\'][">=", "~>"]*.*[\"\'][,]*

Delete it and sleep peacefully.

That’s it. My Gemfile now looks like this:Awsum Gemfile

[I can’t write any further as someone has turned on a 3KHz sound. Stupid neighbours.]


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s