summaryrefslogtreecommitdiff
path: root/doc/syntax
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-07-19 11:18:23 -0700
committerJeremy Evans <code@jeremyevans.net>2019-07-19 11:38:49 -0700
commitbf2f84b2ff298583d3efbecb88da271e99fa7930 (patch)
treee450b733f7abe222d7106211a4bfae650e65b5ca /doc/syntax
parent71d21f3c750f41ab44e618162515bd9e4a9f289e (diff)
Document evaluation order of arguments [ci skip]
Fixes [Misc #8905]
Diffstat (limited to 'doc/syntax')
-rw-r--r--doc/syntax/methods.rdoc19
1 files changed, 19 insertions, 0 deletions
diff --git a/doc/syntax/methods.rdoc b/doc/syntax/methods.rdoc
index 564159ac01f..618eb14a1fb 100644
--- a/doc/syntax/methods.rdoc
+++ b/doc/syntax/methods.rdoc
@@ -283,6 +283,25 @@ This will raise a SyntaxError:
a + b + c
end
+Default argument values can refer to arguments that have already been
+evaluated as local variables, and argument values are always evaluated
+left to right. So this is allowed:
+
+ def add_values(a = 1, b = a)
+ a + b
+ end
+ add_values
+ # => 2
+
+But this will raise a +NameError+ (unless there is a method named
++b+ defined):
+
+ def add_values(a = b, b = 1)
+ a + b
+ end
+ add_values
+ # NameError (undefined local variable or method `b' for main:Object)
+
=== Array Decomposition
You can decompose (unpack or extract values from) an Array using extra