From f13f9f694615780ef0d3d7741e84892d572d5be9 Mon Sep 17 00:00:00 2001 From: zzak Date: Mon, 22 Sep 2014 10:50:52 +0000 Subject: * doc/syntax/methods.rdoc: [DOC] [] and []= methods by @process [Fixes GH-662] https://github.com/ruby/ruby/pull/662 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47685 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- doc/syntax/methods.rdoc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'doc') diff --git a/doc/syntax/methods.rdoc b/doc/syntax/methods.rdoc index 648843e753..58179549f3 100644 --- a/doc/syntax/methods.rdoc +++ b/doc/syntax/methods.rdoc @@ -109,6 +109,25 @@ operator with an @ as in +@ or !@: Unary methods accept zero arguments. +Additionally, methods for element reference and assignment may be defined: +[] and []= respectively. Both can take one or more +arguments, and element reference can take none. + + class C + def [](a, b) + puts a + b + end + + def []=(a, b, c) + puts a * b + c + end + end + + obj = C.new + + obj[2, 3] # prints "5" + obj[2, 3] = 4 # prints "10" + == Return Values By default, a method returns the last expression that was evaluated in the body -- cgit v1.2.3