โ† Ruby Programming learning path
Ruby Programming ยท Module 4 of 10

Methods

Methods package reusable code; the last expression is returned.

๐Ÿ“– Key ideas

  1. def greet(name) ... end defines a method.
  2. The last evaluated expression is returned automatically.
  3. Default arguments: def greet(name = "friend").
  4. Keyword arguments: def pay(amount:, currency: "INR").
  5. Methods ending in ? return true/false; ending in ! often change the object.
๐Ÿ’ก Real-life example: Like a tea recipe card: give it milk and sugar amounts, and it hands back a cup without you saying "return".
defStarts a method definition
Implicit returnLast expression is returned
Keyword argumentArgument passed by name
Bang methodMethod ending with ! that modifies