|
| 1 | +# Def |
| 2 | + |
| 3 | +In Ruby, the `def` keyword is used to define methods. Methods are reusable blocks of code that can accept parameters and return values. Every method implicitly returns the value of its last executed expression. |
| 4 | + |
| 5 | +```ruby |
| 6 | +# Basic method definition |
| 7 | +def greet(name) |
| 8 | + "Hello, #{name}!" |
| 9 | +end |
| 10 | + |
| 11 | +puts greet("Ruby") |
| 12 | +# Output: |
| 13 | +# Hello, Ruby! |
| 14 | +``` |
| 15 | + |
| 16 | +Methods can be defined with different types of parameters, including optional and keyword arguments. |
| 17 | + |
| 18 | +```ruby |
| 19 | +# Method with optional parameter |
| 20 | +def calculate_total(amount, tax = 0.1) |
| 21 | + amount + (amount * tax) |
| 22 | +end |
| 23 | + |
| 24 | +puts calculate_total(100) # Output: 110.0 |
| 25 | +puts calculate_total(100, 0.2) # Output: 120.0 |
| 26 | + |
| 27 | +# Method with keyword arguments |
| 28 | +def create_user(name:, email:, role: "member") |
| 29 | + "#{name} (#{email}) - #{role}" |
| 30 | +end |
| 31 | + |
| 32 | +puts create_user( name: "Alice", email: "[email protected]") |
| 33 | +# Output: Alice ([email protected]) - member |
| 34 | +``` |
| 35 | + |
| 36 | +## Method Return Values |
| 37 | + |
| 38 | +Methods return the value of their last expression by default, but can use an explicit `return` statement to exit early. |
| 39 | + |
| 40 | +```ruby |
| 41 | +def check_status(value) |
| 42 | + return "Invalid" if value < 0 |
| 43 | + |
| 44 | + if value > 100 |
| 45 | + "Too high" |
| 46 | + else |
| 47 | + "OK" |
| 48 | + end |
| 49 | +end |
| 50 | + |
| 51 | +puts check_status(-1) # Output: Invalid |
| 52 | +puts check_status(50) # Output: OK |
| 53 | +puts check_status(150) # Output: Too high |
| 54 | +``` |
| 55 | + |
| 56 | +## Instance and Class Methods |
| 57 | + |
| 58 | +Methods can be defined at both the instance and class level. |
| 59 | + |
| 60 | +```ruby |
| 61 | +class Timer |
| 62 | + # Instance method - called on instances |
| 63 | + def start |
| 64 | + @time = Time.now |
| 65 | + "Timer started" |
| 66 | + end |
| 67 | + |
| 68 | + # Class method - called on the class itself |
| 69 | + def self.now |
| 70 | + Time.now.strftime("%H:%M:%S") |
| 71 | + end |
| 72 | +end |
| 73 | + |
| 74 | +timer = Timer.new |
| 75 | +puts timer.start # Output: Timer started |
| 76 | +puts Timer.now # Output: 14:30:45 |
| 77 | +``` |
| 78 | + |
| 79 | +## Method Visibility |
| 80 | + |
| 81 | +Methods can have different visibility levels using `private`, `protected`, or `public` (default). |
| 82 | + |
| 83 | +```ruby |
| 84 | +class BankAccount |
| 85 | + def initialize(balance) |
| 86 | + @balance = balance |
| 87 | + end |
| 88 | + |
| 89 | + def withdraw(amount) |
| 90 | + return "Insufficient funds" unless sufficient_funds?(amount) |
| 91 | + |
| 92 | + process_withdrawal(amount) |
| 93 | + "Withdrawn: $#{amount}" |
| 94 | + end |
| 95 | + |
| 96 | + private |
| 97 | + |
| 98 | + def sufficient_funds?(amount) |
| 99 | + @balance >= amount |
| 100 | + end |
| 101 | + |
| 102 | + def process_withdrawal(amount) |
| 103 | + @balance -= amount |
| 104 | + end |
| 105 | +end |
| 106 | + |
| 107 | +account = BankAccount.new(100) |
| 108 | +puts account.withdraw(50) # Output: Withdrawn: $50 |
| 109 | +``` |
| 110 | + |
| 111 | +The `def` keyword is essential for organizing code into reusable, maintainable methods that form the building blocks of Ruby programs. |
0 commit comments