liveBook · Manning

Receiver

As explained in The Well-Grounded Rubyist, Fourth Edition

An explicit receiver is the object written before the dot in a Ruby method call.

The object before the dot

In `x = "100".to_i`, the string `"100"` is the receiver: Ruby sends it the message `to_i`, and the resulting integer is assigned to `x`. Calling a method and sending a message describe the same operation from different angles. A message need not match an existing method; an object may handle an unknown message through `method_missing`. section 1.1.4

A method call has a method name and may also have arguments and a code block. When you write a receiver explicitly, a dot must separate it from the method name. When you omit the receiver, you omit the dot too: Ruby uses `self`, the object that is current at that point in execution. Arguments and a block are separate, optional parts of the call. section 6.3.3 section 5.1.4

When a bare call changes meaning

Inside a class definition, `self` is the class object, so a class method such as `C.no_dot` can be called there as `no_dot`. Outside that definition, `C` is no longer `self`; the call needs its explicit receiver, `C.no_dot`. Similarly, an instance method can call another method on the same instance without writing `self`. section 5.1.4

Listing 5.2 Composing whole name from values, using method calls on implicit self
class Person
  attr_accessor :first_name, :middle_name, :last_name
  def whole_name
    n = first_name + " "
    n << "#{middle_name} " if middle_name
    n << last_name                          1
  end
end
david = Person.new
david.first_name = "David"
david.last_name = "Black"
puts "David's whole name: #{david.whole_name}"
david.middle_name = "Alan"
puts "David's new whole name: #{david.whole_name}"

In `whole_name`, bare calls to `first_name`, `middle_name`, and `last_name` use the current `Person` instance as their receiver. Outside the method, calls on `david` name that receiver explicitly. section 5.1.4

The receiver also helps explain two easy-to-miss distinctions:

  • If a local variable and a method share a name, a bare identifier refers to the variable. Use `self.talk` or `talk()` to make the method call unambiguous; avoiding the name clash is simpler. section 5.1.4
  • In `a.concat(other)`, `a` is the array whose contents change; `other` is an argument. Array `+` instead leaves its receiver unchanged. `replace` changes the receiver’s contents, unlike assigning a different array to the variable. section 9.2.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains explicit and default receivers through method-call syntax and worked class and instance examples, without descending into method-dispatch internals.
Examples
Uses string conversion, calls between instance methods, a composed name, and array-combining operations.
What it assumes
Assumes familiarity with Ruby objects and methods while defining the receiver through dot-notation examples.
What you can do after
Diagnose/debug with it by identifying the object a call targets and distinguishing changes to an array receiver from variable reassignment.
How it connects
Connects receivers to `self`, method calls, local-variable name resolution, and array mutation.

Also explained in

Compare how 3 books explain receiver →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.