Calling a Method

You call a method by optionally specifying a receiver, giving the name of the method, and optionally passing some arguments and an optional block. Here’s a code fragment that shows us calling a method with a receiver, a positional argument, a keyword argument, and a block:

 connection.​download_mp3​(​"jitterbug"​, ​speed: :slow​) { |p| show_progress(p) }

In this example, the object connection is the receiver, download_mp3 is the name of the method, the string "jitterbug" is the positional parameter, the key/value pair speed: :slow is a keyword parameter, and the code between the braces is the associated block argument. When the method is called, Ruby invokes the method in that object, and inside that method, self is set to ...

Get Programming Ruby 3.3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.