โ Ruby Programming learning pathRuby Programming ยท Module 4 of 10
Methods
Methods package reusable code; the last expression is returned.
๐ Key ideas
- def greet(name) ... end defines a method.
- The last evaluated expression is returned automatically.
- Default arguments: def greet(name = "friend").
- Keyword arguments: def pay(amount:, currency: "INR").
- 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