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
_ (underscore or low line) or a character with the eight bit
set.

Method names may end with a !
(bang or exclamation mark), a ? (question mark) or
= 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.

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 was the simple sum 1 + 1. The return
keyword can be used to make it explicit that a method returns a value.

defone_plus_onereturn1+1end

It can also be used to make a method return before the last expression is
evaluated.

deftwo_plus_tworeturn2+21+1# this expression is never evaluatedend

Note that for assignment methods the return value will always be ignored.
Instead the argument will be returned:

adds the method to a class. You can define an instance method on a
specific class with the class keyword:

classCdefmy_method# ...endend

A method may be defined on another object. You may define a “class method”
(a method that is defined on the class, not an instance of the class) like
this:

classCdefself.my_method# ...endend

However, this is simply a special case of a greater syntactical power in
Ruby, the ability to add methods to any object. Classes are objects, so
adding class methods is simply adding methods to the Class object.

self is a keyword referring to the current object under
consideration by the compiler, which might make the use of
self in defining a class method above a little clearer.
Indeed, the example of adding a hello method to the class
String can be rewritten thus:

defString.hello"Hello, world!"end

A method defined like this is called a “singleton method”.
broaden will only exist on the string instance
greeting. Other strings will not have broaden.

When Ruby encounters the def keyword, it doesn't consider
it an error if the method already exists: it simply redefines it. This is
called overriding. Rather like extending core classes, this is a
potentially dangerous ability, and should be used sparingly because it can
cause unexpected results. For example, consider this irb session:

A method may accept arguments. The argument list follows the method name:

defadd_one(value)
value+1end

When called, the user of the add_one method must provide an
argument. The argument is a local variable in the method body. The method
will then add one to this argument and return the value. If given
1 this method will return 2.

The parentheses around the arguments are optional:

defadd_onevaluevalue+1end

Multiple arguments are separated by a comma:

defadd_values(a, b)
a+bend

When called, the arguments must be provided in the exact order. In other
words, the arguments are positional.

You can decompose (unpack or extract values from) an Array using extra
parentheses in the arguments:

defmy_method((a, b))
pa:a, b:bendmy_method([1, 2])

This prints:

{:a=>1, :b=>2}

If the argument has extra elements in the Array they will be ignored:

defmy_method((a, b))
pa:a, b:bendmy_method([1, 2, 3])

This has the same output as above.

You can use a * to collect the remaining arguments. This
splits an Array into a first element and the rest:

defmy_method((a, *b))
pa:a, b:bendmy_method([1, 2, 3])

This prints:

{:a=>1, :b=>[2, 3]}

The argument will be decomposed if it responds to to_ary. You should only
define to_ary if you can use your object in place of an Array.

Use of the inner parentheses only uses one of the sent arguments. If the
argument is not an Array it will be assigned to the first argument in the
decomposition and the remaining arguments in the decomposition will be
nil:

Most frequently the block argument is used to pass a block to another
method:

defeach_item(&block)
@items.each(&block)
end

If you are only going to call the block and will not otherwise manipulate
it or send it to another method using yield without an
explicit block parameter is preferred. This method is equivalent to the
first method in this section:

defmy_methodyieldselfend

There is also a performance benefit to using yield over a calling a block
parameter. When a block argument is assigned to a variable a Proc object is created which holds the block. When
using yield this Proc object is not created.

If you only need to use the block sometimes you can use Proc.new to create a proc from the
block that was passed to your method. See Proc.new for further details.