concept method call in category ruby

appears as: method call, method calls, A method call, method call, The method call
The Well-Grounded Rubyist, Third Edition

This is an excerpt from Manning's book The Well-Grounded Rubyist, Third Edition.

The dot means that the message to_i is being sent to the string "100". The string "100" is called the receiver of the message. We can also say that the method to_i is being called on the string "100". The result of the method call—the integer 100—serves as the right-hand side of the assignment to the variable x.

Listing 5.5. Demonstrating the generation of a new local scope per method call
class C
  def x(value_for_a,recurse=false)                  #1
    a = value_for_a                                 #2
    print "Here's the inspect-string for 'self':"   #3
    p self                                          #3
    puts "And here's a:"                            #3
    puts a                                          #3
    if recurse                                      #4
      puts "Calling myself (recursion)..."
      x("Second value for a")                       #5
      puts "Back after recursion; here's a:"        #6
      puts a
    end
  end
end
c = C.new
c.x("First value for a", true)                      #7


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":1,\"ch\":52},{\"line\":1,\"ch\":54}],[{\"line\":2,\"ch\":52},{\"line\":2,\"ch\":54}],[{\"line\":3,\"ch\":52},{\"line\":3,\"ch\":54}],[{\"line\":4,\"ch\":52},{\"line\":4,\"ch\":54}],[{\"line\":5,\"ch\":52},{\"line\":5,\"ch\":54}],[{\"line\":6,\"ch\":52},{\"line\":6,\"ch\":54}],[{\"line\":3,\"ch\":52},{\"line\":3,\"ch\":54}],[{\"line\":4,\"ch\":52},{\"line\":4,\"ch\":54}],[{\"line\":5,\"ch\":52},{\"line\":5,\"ch\":54}],[{\"line\":6,\"ch\":52},{\"line\":6,\"ch\":54}],[{\"line\":3,\"ch\":52},{\"line\":3,\"ch\":54}],[{\"line\":4,\"ch\":52},{\"line\":4,\"ch\":54}],[{\"line\":5,\"ch\":52},{\"line\":5,\"ch\":54}],[{\"line\":6,\"ch\":52},{\"line\":6,\"ch\":54}],[{\"line\":3,\"ch\":52},{\"line\":3,\"ch\":54}],[{\"line\":4,\"ch\":52},{\"line\":4,\"ch\":54}],[{\"line\":5,\"ch\":52},{\"line\":5,\"ch\":54}],[{\"line\":6,\"ch\":52},{\"line\":6,\"ch\":54}],[{\"line\":7,\"ch\":52},{\"line\":7,\"ch\":54}],[{\"line\":9,\"ch\":52},{\"line\":9,\"ch\":54}],[{\"line\":10,\"ch\":52},{\"line\":10,\"ch\":54}],[{\"line\":16,\"ch\":52},{\"line\":16,\"ch\":54}]]"}
!@%STYLE%@!

As you’ve already seen in the case of method calls—where control of the program jumps from the spot where the call is made to the body of the method definition—programs don’t run in a straight line. Instead, execution order is determined by a variety of rules and programming constructs collectively referred to as control-flow techniques.

(The last example shows a block parameter, word. We’ll get back to block parameters presently.) The difference between a method call with a block and a method call without a block comes down to whether or not the method can yield. If there’s a block, then it can; if not, it can’t, because there’s nothing to yield to.

Figure 14.1. A phantom Proc instantiation intervenes between a method call and a method.