Skip to content

Methods

datanorris edited this page Jan 2, 2016 · 47 revisions

Key characteristics of Ruby methods:

  • They are defined on a class or a module
  • They are defined at runtime e.g. by the def statement or by calling the Module.define_method method
  • 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

  • Any combination of "identity characters" followed by ? or !, as long as it doesn't begin with a digit

  • Any combination of "identity characters" followed by =, as long as it doesn't begin with a digit (this is an "attribute set" method)

  • 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 calls - full call style

A "full" method call, in the sense that this uses the full syntax for a method call, looks like this:

1. value.method <optional_block>
2. value.method(optional_arguments) <optional_block>
3. value.method arguments <optional_block>            # command call style

These call method on value with the arguments and block provided. Any method type except attribute set methods can be invoked in this way.

Syntax #3, with no parentheses around the arguments, is called a "command call" and there are syntax grammar restrictions on when you are permitted to use command calls.

Instead of using . you may also use ::, which means the same thing - except if using syntax #1 and the method name begins with an uppercase letter, this will be treated as a constant reference, not a method call (e.g. Object::Class is a constant, while Object.Class and Object::Class() are method calls)

There are also these syntax shorthands, where the method is not specified - the call method will be invoked in these cases:

value.(optional_arguments) <optional block>
value::(optional_arguments) <optional block>

Method calls - function call style

A method call where the object on which the method is invoked is not specified is called a "function call". The method will be invoked on whichever object is currently self.

method(optional_arguments) <optional block>
method arguments <optional block>           # command call style
method <block>

Attribute set methods, operator override methods and methods whose name is a Ruby keyword can't be invoked using this syntax.

Method calls - variable call style

Where a method is invoked using just the method name itself, this is called a variable call because Ruby must decide whether this is a variable reference or a method call.

method

Attribute set methods, operator override methods, methods whose name is a Ruby keyword, methods whose name begins with an uppercase letter and methods whose name is also used by a local variable in scope can't be invoked using this syntax.

Attribute set methods

Attribute set methods have a name ending in =, such as attr=. These are invoked by referencing the "attribute" on the left hand side of an assignment, e.g. the following syntax will invoke the attr= method on obj with 1 argument, value:

obj.attr = value

Any other syntax which performs an assignment in Ruby may also be used (e.g. complex assignment operators such as +=, multiple left hand side assignments).

Again, instead of . you may use ::, unless the first character of the attribute name is uppercase, because this will be interpreted as a constant assignment.

Array reference method

The array reference method, [], can be invoked directly (i.e. obj.[]) however it is typically invoked using the array reference operator:

obj[optional_arguments]

There is not any special restriction on the arguments you may provide inside an array reference operator - arguments can be entirely omitted, or they may contain the full range of method call argument possibilities including splats, hash associations and block arguments. Arguments provided in the array reference operator are passed on to the array reference method directly.

Array set method

The array set method, []=, can be invoked directly (i.e. obj.[]=) however it is typically invoked by using an array reference on the left hand side of an assignment:

obj[optional_arguments] = value

Any other syntax which performs an assignment in Ruby may also be used (e.g. complex assignment operators such as +=, multiple left hand side assignments).

The arguments passed are similar to the array reference method, however in addition the value assigned is appended to the right of all the other arguments except for any block argument provided (but including any hash created by hash associations in the arguments)

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() Arguments to operators

Clone this wiki locally