* lisp/progmodes/ruby-mode.el (ruby-smie-grammar): Improve precedences
[bpt/emacs.git] / test / indent / ruby.rb
CommitLineData
34d1a133
SM
1if something_wrong? # ruby-move-to-block-skips-heredoc
2 ActiveSupport::Deprecation.warn(<<-eowarn)
3 boo hoo
4 end
5 eowarn
dca01b09 6 foo
34d1a133
SM
7end
8
f063063a 9# Percent literals.
dfbd787f 10b = %Q{This is a "string"}
f063063a 11c = %w!foo
dfbd787f 12 bar
f063063a
DG
13 baz!
14d = %(hello (nested) world)
15
16# Don't propertize percent literals inside strings.
17"(%s, %s)" % [123, 456]
18
19# Or inside comments.
20x = # "tot %q/to"; =
a9e4425b 21 y = 2 / 3
f063063a
DG
22
23# Regexp after whitelisted method.
24"abc".sub /b/, 'd'
25
26# Don't mis-match "sub" at the end of words.
27a = asub / aslb + bsub / bslb;
dfbd787f 28
f063063a
DG
29# Highlight the regexp after "if".
30x = toto / foo if /do bar/ =~ "dobar"
dfbd787f 31
34d1a133
SM
32bar(class: XXX) do # ruby-indent-keyword-label
33 foo
34end
35bar
36
37foo = [1, # ruby-deep-indent
38 2]
39
40foo = { # ruby-deep-indent-disabled
5cd9cda9 41 a: b
34d1a133
SM
42}
43
369bbf71 44foo = { a: b,
03d44565
SM
45 a1: b1
46 }
47
8c1ae481
DG
48foo({
49 a: b,
50 c: d
51 })
52
34d1a133
SM
53foo = [ # ruby-deep-indent-disabled
54 1
55]
56
57foo( # ruby-deep-indent-disabled
58 a
59)
60
1a0a0a8a
DG
61# Multiline regexp.
62/bars
63 tees # toots
64 nfoos/
65
a9e4425b
SM
66def test1(arg)
67 puts "hello"
68end
69
70def test2 (arg)
71 a = "apple"
72
73 if a == 2
74 puts "hello"
75 else
76 puts "there"
77 end
78
79 if a == 2 then
80 puts "hello"
81 elsif a == 3
82 puts "hello3"
83 elsif a == 3 then
84 puts "hello3"
85 else
86 puts "there"
87 end
88
89 case a
90 when "a"
91 6
1a0a0a8a
DG
92 # Support for this syntax was removed in Ruby 1.9, so we
93 # probably don't need to handle it either.
a9e4425b
SM
94 # when "b" :
95 # 7
96 # when "c" : 2
97 when "d" then 4
98 else 5
99 end
100end
101
dfbd787f
SM
102# Some Cucumber code:
103Given /toto/ do
104 print "hello"
105end
88527bc0
DG
106
107# Bug#15208
108if something == :==
109 do_something
110end
578c21bc 111
7ccae3b1
SM
112# Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
113d = 4 + 5 + # no '\' needed
114 6 + 7
115
116# Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
117e = 8 + 9 \
118 + 10 # '\' needed
119
1f923923
DG
120foo = obj.bar { |m| tee(m) } +
121 obj.qux { |m| hum(m) }
122
5cd9cda9
DG
123begin
124 foo
125ensure
126 bar
127end
128
578c21bc 129# Bug#15369
2f84ba10 130MSG = 'Separate every 3 digits in the integer portion of a number' \
34d1a133 131 'with underscores(_).'
5cd9cda9 132
7ccae3b1
SM
133class C
134 def foo
135 self.end
136 D.new.class
137 end
138end
5cd9cda9
DG
139
140a = foo(j, k) -
7ccae3b1 141 bar_tee
5cd9cda9
DG
142
143while a < b do # "do" is optional
144 foo
145end
146
147desc "foo foo" \
7ccae3b1 148 "bar bar"
7318480c
DG
149
150foo.
151 bar
152
2f84ba10 153# https://github.com/rails/rails/blob/17f5d8e062909f1fcae25351834d8e89967b645e/activesupport/lib/active_support/time_with_zone.rb#L206
7318480c
DG
154foo
155 .bar
2f84ba10
DG
156
157z = {
158 foo: {
159 a: "aaa",
160 b: "bbb"
161 }
162}
163
238150c8
DG
164foo if
165 bar
166
b68e2926
DG
167if foo?
168 bar
169end
170
1eda1d8d
DG
171method arg1, # bug#15594
172 method2 arg2,
173 arg3
174
175method? arg1,
176 arg2
177
178method! arg1,
179 arg2
180
bae91342
DG
181method !arg1,
182 arg2
183
184method [],
185 arg2
186
187method :foo,
188 :bar
189
190method (a + b),
191 c, :d => :e,
192 f: g
193
8c1ae481
DG
194it "is a method call with block" do |asd|
195 foo
196end
197
198it("is too!") {
199 bar
200}
201
202and_this_one(has) { |block, parameters|
203 tee
204}
205
c7e36328 206if foo &&
cfef16c0
DG
207 bar
208end
209
2f84ba10
DG
210foo +
211 bar
212
1d1c86da
DG
213foo and
214 bar
215
1f923923
DG
216foo > bar &&
217 tee < qux
218
219zux do
220 foo == bar and
221 tee == qux
222end
223
1d1c86da
DG
224foo ^
225 bar
226
18cacc39
DG
227foo_bar_tee(1, 2, 3)
228 .qux
229 .bar
230
a6462ef5
DG
231foo do
232 bar
233 .tee
234end
235
236def bar
237 foo
238 .baz
239end
240
c8c605ac
DG
241# http://stackoverflow.com/questions/17786563/emacs-ruby-mode-if-expressions-indentation
242tee = if foo
243 bar
244 end
245
3d42b968
DG
246a = b {
247 c
248}
249
250aa = bb do
251 cc
252end
253
41784d0b
DG
254foo :bar do
255 qux
256end
257
1f923923
DG
258foo do |*args|
259 tee
260end
261
262bar do |&block|
263 tee
264end
265
238150c8
DG
266foo = [1, 2, 3].map do |i|
267 i + 1
268end
55ee77a3 269
b420ccfc 270bar.foo do
18cacc39 271 bar
18cacc39 272end
1d1c86da 273
1f923923
DG
274bar.foo(tee) do
275 bar
276end
1d1c86da 277
1f923923 278bar.foo(tee) {
1d1c86da 279 bar
1f923923
DG
280}
281
282bar 1 do
283 foo 2 do
284 tee
285 end
1d1c86da 286end