ruby-changes:26773
From: drbrain <ko1@a...>
Date: Tue, 15 Jan 2013 13:33:37 +0900 (JST)
Subject: [ruby-changes:26773] drbrain:r38825 (trunk): * doc/syntax/methods.rdoc (Method Names): Added method names including
drbrain 2013-01-15 13:33:28 +0900 (Tue, 15 Jan 2013) New Revision: 38825 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38825 Log: * doc/syntax/methods.rdoc (Method Names): Added method names including operator methods. * doc/syntax/methods.rdoc (Return Values): Added note that assignment methods ignore return values. * doc/syntax/precedence.rdoc: Added document describing precedence. Added files: trunk/doc/syntax/precedence.rdoc Modified files: trunk/ChangeLog trunk/doc/syntax/methods.rdoc Index: doc/syntax/precedence.rdoc =================================================================== --- doc/syntax/precedence.rdoc (revision 0) +++ doc/syntax/precedence.rdoc (revision 38825) @@ -0,0 +1,60 @@ https://github.com/ruby/ruby/blob/trunk/doc/syntax/precedence.rdoc#L1 += Precedence + +From highest to lowest, this is the precedence table for ruby. High precedence +operations happen before low precedence operations. + + !, ~, unary + + + ** + + unary - + + *, /, % + + +, - + + <<, >> + + & + + |, ^ + + >, >=, <, <= + + <=>, ==, ===, !=, =~, !~ + + && + + || + + .., ... + + ?, : + + modifier-rescue + + =, +=, -=, etc. + + defined? + + not + + or, and + + modifier-if, modifier-unless, modifier-while, modifier-until + + { } blocks + +Unary <code>+</code> and unary <code>-</code> are for <code>+1</code>, +<code>-1</code> or <code>-(a + b)</code>. + +Modifier-if, modifier-unless, etc. are for the modifier versions of those +keywords. For example, this is a modifier-unless expression: + + a += 1 unless a.zero? + +<code>{ ... }</code> blocks have priority below all listed operations, but +<code>do ... end</code> blocks have lower priority. + +All other words in the precedence table above are keywords. + Property changes on: doc/syntax/precedence.rdoc ___________________________________________________________________ Added: svn:eol-style + LF Index: doc/syntax/methods.rdoc =================================================================== --- doc/syntax/methods.rdoc (revision 38824) +++ doc/syntax/methods.rdoc (revision 38825) @@ -11,7 +11,79 @@ A method definition consists of the +def https://github.com/ruby/ruby/blob/trunk/doc/syntax/methods.rdoc#L11 the method, then the +end+ keyword. When called the method will execute the body of the method. This method returns +2+. -== Return values +== Method Names + +Method names may be one of the operators or must start a letter or a character +with the eight bit set. Typically method names are US-ASCII compatible since +the keys to type them exist on all keyboards. + +(Ruby programs must be written in a US-ASCII-compatible character set. In +such character sets if the eight bit is set it indicates an extended +character. Ruby allows method names and other identifiers to contain such +characters.) + +Method names may contain letters, numbers, an <code>_</code> (underscore or +low line) or a character with the eight bit set. + +Method names may end with a <code>!</code> (bang or exclamation mark), a +<code>?</code> (question mark) or <code>=</code> equals sign. + +In the ruby core library when a method ends with a bang it indicates there is +a non-bang method that has does not modify the receiver. This is typically +true for the standard library but does not hold true for other ruby libraries. + +Methods that end with a question mark do not always return just +true+ or ++false+. Often they will may return an object to indicate a true value (or +"truthy" value). + +Methods that end with an equals sign indicate an assignment method. For +assignment methods the return value is ignored, the arguments are returned +instead. + +These are method names for the various ruby operators. Each of these +operators accept only one argument. Following the operator is the typical +use or name of the operator. Creating an alternate meaning for the operator +may lead to confusion as the user expects plus to add things, minus to +subtract things, etc. Additionally, you cannot alter the precedence of the +operators. + +<code>+</code> :: add +<code>-</code> :: subtract +<code>*</code> :: multiply +<code>**</code> :: power +<code>/</code> :: divide +<code>%</code> :: modulus division, String#% +<code>&</code> :: AND +<code>^</code> :: XOR (exclusive OR) +<code>>></code> :: right-shift +<code><<</code> :: left-shift, append +<code>==</code> :: equal +<code>!=</code> :: not equal +<code>===</code> :: case equality. See Object#=== +<code>=~</code> :: pattern match. (Not just for regular expressions) +<code>!~</code> :: does not match +<code><=></code> :: comparison aka spaceship operator. See Comparable +<code><</code> :: less-than +<code><=</code> :: less-than or equal +<code>></code> :: greater-than +<code>>=</code> :: greater-than or equal + +To define unary methods minus, plus, tilde and not (<code>!</code>) follow the +operator with an <code>@</code> as in <code>+@</code> or <code>!@<code>: + + class C + def -@ + puts "you inverted this object" + end + end + + obj = C.new + + -obj # prints "you inverted this object" + +Unary methods accept zero arguments. + +== Return Values By default, a method returns the last expression that was evaluated in the body of the method. In the example above, the last (and only) expression evaluated @@ -30,6 +102,15 @@ evaluated. https://github.com/ruby/ruby/blob/trunk/doc/syntax/methods.rdoc#L102 1 + 1 # this expression is never evaluated end +Note that for assignment methods the return value will always be ignored. +Instead the argument will be returned: + + def a=(value) + return 1 + value + end + + p(a = 5) # prints 5 + == Scope The standard syntax to define a method: Index: ChangeLog =================================================================== --- ChangeLog (revision 38824) +++ ChangeLog (revision 38825) @@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Jan 15 13:33:00 2013 Eric Hodel <drbrain@s...> + + * doc/syntax/methods.rdoc (Method Names): Added method names including + operator methods. + * doc/syntax/methods.rdoc (Return Values): Added note that assignment + methods ignore return values. + * doc/syntax/precedence.rdoc: Added document describing precedence. + Tue Jan 15 11:49:31 2013 Eric Hodel <drbrain@s...> * doc/syntax/methods.rdoc (Block Argument): Added section on block -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/