|
| 1 | +# Class |
| 2 | + |
| 3 | +In Ruby, a `class` is a blueprint for creating objects that share similar attributes and behaviors. Classes encapsulate data and methods, following object-oriented programming principles. |
| 4 | + |
| 5 | +```ruby |
| 6 | +# Basic class definition |
| 7 | +class Person |
| 8 | + def initialize(name) |
| 9 | + @name = name |
| 10 | + end |
| 11 | + |
| 12 | + def greet |
| 13 | + puts "Hello, #{@name}!" |
| 14 | + end |
| 15 | +end |
| 16 | + |
| 17 | +person = Person.new("Ruby") |
| 18 | +person.greet |
| 19 | +# Output: |
| 20 | +# Hello, Ruby! |
| 21 | +``` |
| 22 | + |
| 23 | +Classes can include instance methods, class methods, and various types of variables. |
| 24 | + |
| 25 | +```ruby |
| 26 | +class Product |
| 27 | + # Class variable (shared across all instances) |
| 28 | + @@count = 0 |
| 29 | + |
| 30 | + # Class method |
| 31 | + def self.count |
| 32 | + @@count |
| 33 | + end |
| 34 | + |
| 35 | + def initialize(name, price) |
| 36 | + @name = name |
| 37 | + @price = price |
| 38 | + @@count += 1 |
| 39 | + end |
| 40 | + |
| 41 | + # Instance method |
| 42 | + def details |
| 43 | + "#{@name}: $#{@price}" |
| 44 | + end |
| 45 | +end |
| 46 | + |
| 47 | +book = Product.new("Ruby Guide", 29.99) |
| 48 | +puts Product.count # Output: 1 |
| 49 | +puts book.details # Output: Ruby Guide: $29.99 |
| 50 | +``` |
| 51 | + |
| 52 | +## Inheritance |
| 53 | + |
| 54 | +Classes can inherit behavior from other classes using the `<` operator. A class can only inherit from one parent class. |
| 55 | + |
| 56 | +```ruby |
| 57 | +# Parent class |
| 58 | +class Animal |
| 59 | + def speak |
| 60 | + "Some sound" |
| 61 | + end |
| 62 | +end |
| 63 | + |
| 64 | +# Child class |
| 65 | +class Dog < Animal |
| 66 | + def speak |
| 67 | + "Woof!" |
| 68 | + end |
| 69 | +end |
| 70 | + |
| 71 | +dog = Dog.new |
| 72 | +puts dog.speak # Output: Woof! |
| 73 | +``` |
| 74 | + |
| 75 | +## Access Control |
| 76 | + |
| 77 | +Ruby provides three levels of method access control: `public`, `private`, and `protected`. |
| 78 | + |
| 79 | +```ruby |
| 80 | +class BankAccount |
| 81 | + def initialize(balance) |
| 82 | + @balance = balance |
| 83 | + end |
| 84 | + |
| 85 | + # Public method - can be called by anyone |
| 86 | + def display_balance |
| 87 | + "Current balance: $#{@balance}" |
| 88 | + end |
| 89 | + |
| 90 | + # Protected method - can be called by other instances |
| 91 | + protected |
| 92 | + |
| 93 | + def compare_balance(other) |
| 94 | + @balance > other.balance |
| 95 | + end |
| 96 | + |
| 97 | + # Private method - can only be called internally |
| 98 | + private |
| 99 | + |
| 100 | + def update_balance(amount) |
| 101 | + @balance += amount |
| 102 | + end |
| 103 | +end |
| 104 | + |
| 105 | +account = BankAccount.new(100) |
| 106 | +puts account.display_balance |
| 107 | +# Output: Current balance: $100 |
| 108 | +``` |
| 109 | + |
| 110 | +## Class Instance Variables |
| 111 | + |
| 112 | +Instance variables can be exposed using attribute accessors. Ruby provides several methods to create them. |
| 113 | + |
| 114 | +```ruby |
| 115 | +class User |
| 116 | + # Create reader and writer methods |
| 117 | + attr_accessor :name |
| 118 | + |
| 119 | + # Create reader only |
| 120 | + attr_reader :created_at |
| 121 | + |
| 122 | + # Create writer only |
| 123 | + attr_writer :password |
| 124 | + |
| 125 | + def initialize(name) |
| 126 | + @name = name |
| 127 | + @created_at = Time.now |
| 128 | + end |
| 129 | +end |
| 130 | + |
| 131 | +user = User.new("Alice") |
| 132 | +puts user.name # Output: Alice |
| 133 | +user.name = "Bob" |
| 134 | +puts user.name # Output: Bob |
| 135 | +``` |
| 136 | + |
| 137 | +The `class` keyword is fundamental to Ruby's object-oriented nature, allowing you to create organized, reusable, and maintainable code through encapsulation, inheritance, and polymorphism. |
0 commit comments