aboutsummaryrefslogtreecommitdiff
path: root/vim/bundle/vim-snippets/UltiSnips/ruby.snippets
blob: b7124f83bf83d10dd2723654171a5d9c0874d91f (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
priority -50

snippet "^#!" "#!/usr/bin/env ruby" r
#!/usr/bin/env ruby
$0
endsnippet

snippet "^# ?[uU][tT][fF]-?8" "# encoding: UTF-8" r
# encoding: UTF-8
$0
endsnippet

snippet "\b(de)?f" "def <name>..." r
def ${1:function_name}${2:(${3:*args})}
	$0
end
endsnippet

snippet defi "def initialize ..."
def initialize${1:(${2:*args})}
	$0
end
endsnippet

snippet defr "def <name> ... rescue ..."
def ${1:function_name}${2:(${3:*args})}
	$4
rescue
	$0
end
endsnippet

snippet For "(<from>..<to>).each { |<i>| <block> }"
(${1:from}..${2:to}).each { |${3:i}| $0 }
endsnippet

snippet "(\S+)\.Merge!" ".merge!(<other_hash>) { |<key>,<oldval>,<newval>| <block> }" r
`!p snip.rv=match.group(1)`.merge!(${1:other_hash}) { |${2:key},${3:oldval},${4:newval}| ${5:block} }
endsnippet

snippet "(\S+)\.merge!" ".merge!(<other_hash>) do |<key>,<oldval>,<newval>| <block> end" r
`!p snip.rv=match.group(1)`.merge!(${1:other_hash}) do |${2:key},${3:oldval},${4:newval}|
	${0:block}
end
endsnippet

snippet "(\S+)\.Del(ete)?_?if" ".delete_if { |<key>,<value>| <block> }" r
`!p snip.rv=match.group(1)`.delete_if { |${1:key},${2:value}| $0 }
endsnippet

snippet "(\S+)\.del(ete)?_?if" ".delete_if do |<key>,<value>| <block> end" r
`!p snip.rv=match.group(1)`.delete_if do |${1:key},${2:value}|
	$0
end
endsnippet

snippet "(\S+)\.Keep_?if" ".keep_if { |<key>,<value>| <block> }" r
`!p snip.rv=match.group(1)`.keep_if { |${1:key},${2:value}| $0 }
endsnippet

snippet "(\S+)\.keep_?if" ".keep_if do <key>,<value>| <block> end" r
`!p snip.rv=match.group(1)`.keep_if do |${1:key},${2:value}|
	$0
end
endsnippet

snippet "(\S+)\.Reject" ".reject { |<key>,<value>| <block> }" r
`!p snip.rv=match.group(1)`.reject { |${1:key},${2:value}| $0 }
endsnippet

snippet "(\S+)\.reject" ".reject do <key>,<value>| <block> end" r
`!p snip.rv=match.group(1)`.reject do |${1:key},${2:value}|
	$0
end
endsnippet

snippet "(\S+)\.Select" ".select { |<item>| <block>}" r
`!p snip.rv=match.group(1)`.select { |${1:item}| ${2:block} }
endsnippet

snippet "(\S+)\.select" ".select do |<item>| <block> end" r
`!p snip.rv=match.group(1)`.select do |${1:item}|
	${0:block}
end
endsnippet

snippet "(\S+)\.Sort" ".sort { |<a>,<b>| <block> }" r
`!p snip.rv=match.group(1)`.sort { |${1:a},${2:b}| $0 }
endsnippet

snippet "(\S+)\.sort" ".sort do |<a>,<b>| <block> end" r
`!p snip.rv=match.group(1)`.sort do |${1:a},${2:b}|
	$0
end
endsnippet

snippet "(\S+)\.Each_?k(ey)?" ".each_key { |<key>| <block> }" r
`!p snip.rv=match.group(1)`.each_key { |${1:key}| $0 }
endsnippet

snippet "(\S+)\.each_?k(ey)?" ".each_key do |key| <block> end" r
`!p snip.rv=match.group(1)`.each_key do |${1:key}|
	$0
end
endsnippet

snippet "(\S+)\.Each_?val(ue)?" ".each_value { |<value>| <block> }" r
`!p snip.rv=match.group(1)`.each_value { |${1:value}| $0 }
endsnippet

snippet "(\S+)\.each_?val(ue)?" ".each_value do |<value>| <block> end" r
`!p snip.rv=match.group(1)`.each_value do |${1:value}|
	$0
end
endsnippet

snippet "(\S+)\.ea" "<elements>.each do |<element>| <block> end" r
`!p snip.rv=match.group(1)`.each { |${1:e}| $0 }
endsnippet

snippet "(\S+)\.ead" "<elements>.each do |<element>| <block> end" r
`!p snip.rv=match.group(1)`.each do |${1:e}|
	$0
end
endsnippet

snippet "each_?s(lice)?" "<array>.each_slice(n) do |slice| <block> end" r
${1:elements}.each_slice(${2:2}) do |${3:slice}|
	$0
end
endsnippet

snippet "Each_?s(lice)?" "<array>.each_slice(n) { |slice| <block> }" r
${1:elements}.each_slice(${2:2}) { |${3:slice}| $0 }
endsnippet

snippet "(\S+)\.Map" ".map { |<element>| <block> }" r
`!p snip.rv=match.group(1)`.map { |${1:`!p
element_name = match.group(1).lstrip('$@')
ematch = re.search("([A-Za-z][A-Za-z0-9_]+?)s?[^A-Za-z0-9_]*?$", element_name)
try:
	wmatch = re.search("([A-Za-z][A-Za-z0-9_]+)$", ematch.group(1))
	snip.rv = wmatch.group(1).lower()
except:
	snip.rv = 'element'
`}| $0 }
endsnippet

snippet "(\S+)\.map" ".map do |<element>| <block> end" r
`!p snip.rv=match.group(1)`.map do |${1:`!p
element_name = match.group(1).lstrip('$@')
ematch = re.search("([A-Za-z][A-Za-z0-9_]+?)s?[^A-Za-z0-9_]*?$", element_name)
try:
	wmatch = re.search("([A-Za-z][A-Za-z0-9_]+)$", ematch.group(1))
	snip.rv = wmatch.group(1).lower()
except:
	snip.rv = 'element'
`}|
	$0
end
endsnippet

snippet "(\S+)\.Rev(erse)?_?each" ".reverse_each { |<element>| <block> }" r
`!p snip.rv=match.group(1)`.reverse_each { |${1:`!p
element_name = match.group(1).lstrip('$@')
ematch = re.search("([A-Za-z][A-Za-z0-9_]+?)s?[^A-Za-z0-9_]*?$", element_name)
try:
	wmatch = re.search("([A-Za-z][A-Za-z0-9_]+)$", ematch.group(1))
	snip.rv = wmatch.group(1).lower()
except:
	snip.rv = 'element'
`}| $0 }
endsnippet

snippet "(\S+)\.rev(erse)?_?each" ".reverse_each do |<element>| <block> end" r
`!p snip.rv=match.group(1)`.reverse_each do |${1:`!p
element_name = match.group(1).lstrip('$@')
ematch = re.search("([A-Za-z][A-Za-z0-9_]+?)s?[^A-Za-z0-9_]*?$", element_name)
try:
	wmatch = re.search("([A-Za-z][A-Za-z0-9_]+)$", ematch.group(1))
	snip.rv = wmatch.group(1).lower()
except:
	snip.rv = 'element'
`}|
	$0
end
endsnippet

snippet "(\S+)\.Each" ".each { |<element>| <block> }" r
`!p snip.rv=match.group(1)`.each { |${1:`!p
element_name = match.group(1).lstrip('$@')
ematch = re.search("([A-Za-z][A-Za-z0-9_]+?)s?[^A-Za-z0-9_]*?$", element_name)
try:
	wmatch = re.search("([A-Za-z][A-Za-z0-9_]+)$", ematch.group(1))
	snip.rv = wmatch.group(1).lower()
except:
	snip.rv = 'element'
`}| $0 }
endsnippet

snippet "(\S+)\.each" ".each do |<element>| <block> end" r
`!p snip.rv=match.group(1)`.each do |${1:`!p
element_name = match.group(1).lstrip('$@')
ematch = re.search("([A-Za-z][A-Za-z0-9_]+?)s?[^A-Za-z0-9_]*?$", element_name)
try:
	wmatch = re.search("([A-Za-z][A-Za-z0-9_]+)$", ematch.group(1))
	snip.rv = wmatch.group(1).lower()
except:
	snip.rv = 'element'
`}|
	$0
end
endsnippet

snippet "(\S+)\.Each_?w(ith)?_?i(ndex)?" ".each_with_index { |<element>,<i>| <block> }" r
`!p snip.rv=match.group(1)`.each_with_index { |${1:`!p
element_name = match.group(1).lstrip('$@')
ematch = re.search("([A-Za-z][A-Za-z0-9_]+?)s?[^A-Za-z0-9_]*?$", element_name)
try:
	wmatch = re.search("([A-Za-z][A-Za-z0-9_]+)$", ematch.group(1))
	snip.rv = wmatch.group(1).lower()
except:
	snip.rv = 'element'
`},${2:i}| $0 }
endsnippet

snippet "(\S+)\.each_?w(ith)?_?i(ndex)?" ".each_with_index do |<element>,<i>| <block> end" r
`!p snip.rv=match.group(1)`.each_with_index do |${1:`!p
element_name = match.group(1).lstrip('$@')
ematch = re.search("([A-Za-z][A-Za-z0-9_]+?)s?[^A-Za-z0-9_]*?$", element_name)
try:
	wmatch = re.search("([A-Za-z][A-Za-z0-9_]+)$", ematch.group(1))
	snip.rv = wmatch.group(1).lower()
except:
	snip.rv = 'element'
`},${2:i}|
	$0
end
endsnippet

snippet "(\S+)\.Each_?p(air)?"  ".each_pair { |<key>,<value>| <block> }" r
`!p snip.rv=match.group(1)`.each_pair { |${1:key},${2:value}| $0 }
endsnippet

snippet "(\S+)\.each_?p(air)?" ".each_pair do |<key>,<value>| <block> end" r
`!p snip.rv=match.group(1)`.each_pair do |${1:key},${2:value}|
	$0
end
endsnippet

snippet "(\S+)\.sub" ".sub(<expression>) { <block> }" r
`!p snip.rv=match.group(1)`.sub(${1:expression}) { ${2:"replace_with"} }
endsnippet

snippet "(\S+)\.gsub" ".gsub(<expression>) { <block> }" r
`!p snip.rv=match.group(1)`.gsub(${1:expression}) { ${2:"replace_with"} }
endsnippet

snippet "(\S+)\.index" ".index { |item| <block> }" r
`!p snip.rv=match.group(1)`.index { |${1:item}| ${2:block} }
endsnippet

snippet "(\S+)\.Index" ".index do |item| ... end" r
`!p snip.rv=match.group(1)`.index do |${1:item}|
	${0:block}
end
endsnippet

snippet until "until <expression> ... end"
until ${1:expression}
	$0
end
endsnippet

snippet Until "begin ... end until <expression>"
begin
	$0
end until ${1:expression}
endsnippet

snippet while "while <expression> ... end"
while ${1:expression}
	$0
end
endsnippet

snippet While "begin ... end while <expression>"
begin
	$0
end while ${1:expression}
endsnippet

snippet begin "begin ... rescue ... end"
begin
	$1
rescue
	$0
end
endsnippet

snippet begin "begin ... rescue ... else ... ensure ... end"
begin
	${1:# Raise exception}
rescue Exception => e
	puts e.message
	puts e.backtrace.inspect
	${2:# Rescue}
else
	${3:# other exception}
ensure
	${0:# always excute}
end
endsnippet

snippet rescue
rescue Exception => e
	puts e.message
	puts e.backtrace.inspect
	${0:# Rescue}
endsnippet

snippet "\b(case|sw(itch)?)" "case <variable> when <expression> ... end" r
case ${1:variable}
when ${2:expression}
$0
end
endsnippet

snippet alias "alias :<new_name> :<old_name>"
alias :${1:new_name} :${2:old_name}
endsnippet

snippet class "class <class_name> def initialize ... end end"
class ${1:class_name}
	def initialize(${2:*args})
		$0
	end
end
endsnippet

snippet module "module"
module ${1:module_name}
	$0
end
endsnippet

snippet ###
=begin
	$0
=end
endsnippet

# vim: set ts=2 sw=2 expandtab: