-
Notifications
You must be signed in to change notification settings - Fork 1
Methods
Key characteristics of Ruby methods:
- They are defined on a class or a module
- They are defined at runtime e.g. by the
defstatement or by calling theModule.define_methodmethod - Methods can also be redefined, removed or undefined
- Method calls are invoked on an object, and resolved at runtime using a "message-passing" paradigm - at the time of method call, the object's class and its ancestor classes are searched until one is found which has the method defined on it
- A method's "signature" is just its name - there is no overloading to allow method calls on the same method with different arguments to be resolved to different implementations
- They have powerful and flexible options for passing arguments
- The name of a method can be an operator, such as
+, in order to define the behaviour of the operator for a class/module
#Method names A method name may be:
-
Any combination of "identity characters" (i.e. alphanumeric,
_or non-ASCII characters), as long as it doesn't begin with a digit -
The name can be one of Ruby's 41 reserved words (e.g.
class), but there are restrictions on what syntax you can use to call it -
The name may begin with an uppercase letter (which is a name normally interpreted as a constant), but there are restrictions on what syntax you can use to call it
-
One of the following operators:
| (binary bitwise or) ^ (binary bitwise xor) & (binary bitwise and) <=> (comparison operator) == (equal to) === (case expression match) =~ (match) !~ (not match) > (greater than) >= (greater than or equal to) < (less than) <= (less than or equal to) != (not equal to) << (bitwise left shift) >> (bitwise right shift) + (binary plus) - (binary minus) * (multiply) / (divide) % (modulus) ** (power) ! (boolean unary not) ~ (complement) -
One of the following tokens
+@ (defines unary plus e.g. +value) -@ (defines unary minus e.g. -value) [] (defines array reference get e.g. value[1]) []= (defines array reference set e.g. value[1] = 'hello') ` (defines processing of backquote literal) !@ (same as !) ~@ (same as ~)
Method signature and resolution Method missing Method invocation styles (method & command call) Method formal arguments Method call arguments Permitted method names Undefining Blocks and block arguments send()
A Ruby Language Reference
Copyright © by Michael Hore, 2016.
Introduction to This Document
Ruby Elements
- Classes and Modules
- Methods
- Blocks, Procs and Lambdas
- Execution Context and Closures
- Variables, Constants and Namespaces
- Types and Literals
- Ruby Expressions
- Operators
Syntax Grammar
Exceptions and Throw
Ruby Sourcefiles and Libraries
Multi Threading
Execution and Lifecycle