You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
3
+
In Ruby, a `class` is a blueprint for creating objects that encapsulate related state and behavior. Each instance of a class has its own set of instance variables and methods, allowing objects to maintain their individual state.
4
4
5
5
```ruby
6
6
# Basic class definition
7
7
classPerson
8
8
definitialize(name)
9
-
@name= name
9
+
@name= name# Instance variable stores state
10
10
end
11
11
12
-
defgreet
12
+
defgreet# Instance method stores behavior
13
13
puts"Hello, #{@name}!"
14
14
end
15
15
end
@@ -20,108 +20,43 @@ person.greet
20
20
# Hello, Ruby!
21
21
```
22
22
23
-
Classes can include instance methods, class methods, and various types of variables.
24
-
25
-
```ruby
26
-
classProduct
27
-
# Class variable (shared across all instances)
28
-
@@count=0
29
-
30
-
# Class method
31
-
class << self
32
-
defcount
33
-
@@count
34
-
end
35
-
end
23
+
## Instance Variables and Methods
36
24
37
-
definitialize(name, price)
38
-
@name= name
39
-
@price= price
40
-
@@count+=1
41
-
end
42
-
43
-
# Instance method
44
-
defdetails
45
-
"#{@name}: $#{@price}"
46
-
end
47
-
end
48
-
49
-
book =Product.new("Ruby Guide", 29.99)
50
-
putsProduct.count # Output: 1
51
-
puts book.details # Output: Ruby Guide: $29.99
52
-
```
53
-
54
-
## Inheritance
55
-
56
-
Classes can inherit behavior from other classes using the `<` operator. A class can only inherit from one parent class.
57
-
58
-
```ruby
59
-
# Parent class
60
-
classAnimal
61
-
defspeak
62
-
"Some sound"
63
-
end
64
-
end
65
-
66
-
# Child class
67
-
classDog < Animal
68
-
defspeak
69
-
"Woof!"
70
-
end
71
-
end
72
-
73
-
dog =Dog.new
74
-
puts dog.speak # Output: Woof!
75
-
```
76
-
77
-
## Access Control
78
-
79
-
Ruby provides three levels of method access control: `public`, `private`, and `protected`.
25
+
Instance variables (starting with `@`) store object-specific state, while instance methods define the behavior that each object can perform.
80
26
81
27
```ruby
82
28
classBankAccount
83
29
definitialize(balance)
84
30
@balance= balance
85
31
end
86
32
87
-
# Public method - can be called by anyone
88
-
defdisplay_balance
89
-
"Current balance: $#{@balance}"
90
-
end
91
-
92
-
# Protected method - can be called by other instances
93
-
protected
94
-
95
-
defcompare_balance(other)
96
-
@balance> other.balance
33
+
defdeposit(amount)
34
+
@balance+= amount
97
35
end
98
36
99
-
# Private method - can only be called internally
100
-
private
101
-
102
-
defupdate_balance(amount)
103
-
@balance+= amount
37
+
defcurrent_balance
38
+
@balance
104
39
end
105
40
end
106
41
107
42
account =BankAccount.new(100)
108
-
putsaccount.display_balance
109
-
# Output: Current balance: $100
43
+
account.deposit(50)
44
+
puts account.current_balance # Output: 150
110
45
```
111
46
112
-
## Class Instance Variables
47
+
## Attribute Accessors
113
48
114
-
Instance variables can be exposed using attribute accessors. Ruby provides several methods to create them.
49
+
Ruby provides convenient methods to create getters and setters for instance variables:
115
50
116
51
```ruby
117
52
classUser
118
-
#Create reader and writer methods
53
+
#Creates both getter and setter methods
119
54
attr_accessor:name
120
55
121
-
#Create reader only
56
+
#Creates getter method only
122
57
attr_reader:created_at
123
58
124
-
#Create writer only
59
+
#Creates setter method only
125
60
attr_writer:password
126
61
127
62
definitialize(name)
@@ -131,9 +66,36 @@ class User
131
66
end
132
67
133
68
user =User.new("Alice")
134
-
puts user.name # Output: Alice
135
-
user.name ="Bob"
136
-
puts user.name # Output: Bob
69
+
puts user.name # Using getter (Output: Alice)
70
+
user.name ="Bob"# Using setter
71
+
puts user.name # Output: Bob
72
+
puts user.created_at # Using reader
73
+
user.password ="123"# Using writer
74
+
```
75
+
76
+
## Inheritance
77
+
78
+
Classes can inherit behavior from other classes using the `<` operator, allowing for code reuse and specialization.
79
+
80
+
```ruby
81
+
classAnimal
82
+
definitialize(name)
83
+
@name= name
84
+
end
85
+
86
+
defspeak
87
+
"Some sound"
88
+
end
89
+
end
90
+
91
+
classDog < Animal
92
+
defspeak
93
+
"#{@name} says: Woof!"
94
+
end
95
+
end
96
+
97
+
dog =Dog.new("Rex")
98
+
puts dog.speak # Output: Rex says: Woof!
137
99
```
138
100
139
-
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.
101
+
The `class` keyword is fundamental to Ruby's object-oriented nature, allowing you to create organized, reusablecode by grouping related data and behavior into objects.
0 commit comments