Cory O'Daniel – These are just words Software development, thoughts, and randomness

1Feb/100

Symbol to Proc Textmate Find/Replace (reversal) macro

So I know some people are the fans of the symbol to proc in ruby and others aren't. I'm not. I'm all for doing things to save developer time, but I think the symbol to proc thing is super lazy. I see it a lot in projects I work on from other developers and I usually remove it when I see it. So I wrote a textmate find and replace macro to undo them. You can download it here or copy the following source into a file called whatever.tmMacro.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>bundleUUID</key>
	<string>D52BDC31-EFB7-486E-8232-F5B5517E2DF8</string>
	<key>commands</key>
	<array>
		<dict>
			<key>argument</key>
			<dict>
				<key>action</key>
				<string>replaceAll</string>
				<key>findInProjectIgnoreCase</key>
				<false/>
				<key>findString</key>
				<string>(\(?)( ?)&amp;:(\w+)( ?)(\)?)</string>
				<key>ignoreCase</key>
				<true/>
				<key>replaceAllScope</key>
				<string>document</string>
				<key>replaceString</key>
				<string>{|i| i.$3 }</string>
				<key>wrapAround</key>
				<true/>
			</dict>
			<key>command</key>
			<string>findWithOptions:</string>
		</dict>
	</array>
	<key>keyEquivalent</key>
	<string>@P</string>
	<key>name</key>
	<string>SymbolToProcToNonLazy</string>
	<key>scope</key>
	<string>source.ruby</string>
	<key>useGlobalClipboard</key>
	<false/>
	<key>uuid</key>
	<string>94D8367E-8C6A-4920-AD8D-125CBEFD063D</string>
</dict>
</plist>

Once its installed you should be able to do Command-Shift-P and replace all the symbol-to-procs in the current document with their regular proc equivalent.

I've tested it with the following scenarios:

1
2
3
%w(My super cool array).map &:to_s
%w(My super cool array).map( &:to_s )
%w(My super cool array).map(&:to_s)

If you have any other crazy way of passing in your symbol to proc, let me know. Of course if this macro destroys your computer or sets your hair on fire I take no responsibility, just ask my illegitimate children.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon