Skip to content

Commit 0d6d65c

Browse files
dvandersluisbbatsov
authored andcommitted
Add section on ambiguous endless method definitions.
1 parent e66a93a commit 0d6d65c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

README.adoc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2907,6 +2907,29 @@ def set_x(x) = (@x = x)
29072907
def print_foo = puts("foo")
29082908
----
29092909

2910+
=== Ambiguous Endless Method Definitions [[ambiguous-endless-method-defintions]]
2911+
2912+
Keywords with lower precedence than `=` can appear ambiguous when used after an endless method definition. This includes `and`, `or`, and the modifier forms of `if`, `unless`, `while`, and `until`. In these cases, the code may appear to include these keywords as part of the method body, but instead they actually modify the method definition itself.
2913+
2914+
In this cases, prefer using a normal method over an endless method.
2915+
2916+
[source,ruby]
2917+
----
2918+
# bad
2919+
def foo = true if bar
2920+
2921+
# good - using a non-endless method is more explicit
2922+
def foo
2923+
true
2924+
end if bar
2925+
2926+
# ok - method body is explicit
2927+
def foo = (true if bar)
2928+
2929+
# ok - method definition is explicit
2930+
(def foo = true) if bar
2931+
----
2932+
29102933
=== Double Colons [[double-colons]]
29112934

29122935
Use `::` only to reference constants (this includes classes and modules) and constructors (like `Array()` or `Nokogiri::HTML()`).

0 commit comments

Comments
 (0)