Methods implement the functionality of your program. Here is a simple
method definition:

defone_plus_one1+1end

A method definition consists of the def keyword, a method
name, the body of the method, return value and the
end keyword. When called the method will execute the body of
the method. This method returns 2.

This section only covers defining methods. See also the syntax
documentation on calling methods.

Method names may be one of the operators or
must start a letter or a character with the eight bit set. It may contain
letters, numbers, an _ (underscore or low line) or a character
with the eight bit set. The convention is to use underscores to separate
words in a multiword method name:

defmethod_nameputs"use underscores to separate words"end

Ruby programs must be written in a US-ASCII-compatible character set such
as UTF-8, ISO-8859-1 etc. 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. Ruby programs cannot contain some
characters like ASCII NUL (\x00).

The following are the examples of valid ruby methods:

defhello"hello"enddefこんにちはputs"means hello in Japanese"end

Typically method names are US-ASCII compatible since the keys to type them
exist on all keyboards.

Method names may end with a !
(bang or exclamation mark), a ? (question mark) or
= equals sign.

The bang methods (! at the end of method name) are called and
executed just like any other method. However, by convention, a method with
an exclamation point or bang is considered dangerous. In ruby core library
the dangerous method implies that when a method ends with a bang
(!), it indicates that unlike its non-bang equivalent,
permanently modifies its receiver. Almost always, ruby core library will
have a non-bang counterpart (method name which does NOT end with
!) of every bang method (method name which does end with
!) that does not modify the receiver. This convention is
typically true for ruby core library but may or may not hold true for other
ruby libraries.

Methods that end with a question mark by convention return boolean, but
they may not always return just true or false.
Often, they will 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 and 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 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.