Skip to content

Commit ae11a22

Browse files
committed
Revise docs to be more focused
1 parent d554850 commit ae11a22

File tree

2 files changed

+92
-85
lines changed

2 files changed

+92
-85
lines changed

static_docs/case.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,51 @@ else
4646
end
4747
```
4848

49+
## Type and Module Checking
50+
51+
`case` statements are commonly used to check an object's type or included modules, leveraging Ruby's `===` operator.
52+
53+
```ruby
54+
# Checking inheritance and module inclusion
55+
module Printable
56+
def print_info
57+
puts "[INFO] #{to_s}"
58+
end
59+
end
60+
61+
class Report
62+
include Printable
63+
end
64+
65+
class User
66+
end
67+
68+
object = Report.new
69+
70+
case object
71+
when Printable
72+
object.print_info
73+
when User
74+
puts "Found a user"
75+
else
76+
puts "Unknown object type"
77+
end
78+
# Output: [INFO] #<Report:0x00007f8>
79+
80+
# Multiple type checks
81+
result = 42
82+
83+
case result
84+
when String
85+
puts "Got a string: #{result}"
86+
when Integer
87+
puts "Got a number: #{result}"
88+
when Array
89+
puts "Got an array with #{result.length} items"
90+
end
91+
# Output: Got a number: 42
92+
```
93+
4994
## Pattern Matching
5095

5196
`case` statements support pattern matching, which provides powerful ways to match and destructure data.

static_docs/class.md

Lines changed: 47 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Class
22

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.
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.
44

55
```ruby
66
# Basic class definition
77
class Person
88
def initialize(name)
9-
@name = name
9+
@name = name # Instance variable stores state
1010
end
1111

12-
def greet
12+
def greet # Instance method stores behavior
1313
puts "Hello, #{@name}!"
1414
end
1515
end
@@ -20,108 +20,43 @@ person.greet
2020
# Hello, Ruby!
2121
```
2222

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-
class << self
32-
def count
33-
@@count
34-
end
35-
end
23+
## Instance Variables and Methods
3624

37-
def initialize(name, price)
38-
@name = name
39-
@price = price
40-
@@count += 1
41-
end
42-
43-
# Instance method
44-
def details
45-
"#{@name}: $#{@price}"
46-
end
47-
end
48-
49-
book = Product.new("Ruby Guide", 29.99)
50-
puts Product.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-
class Animal
61-
def speak
62-
"Some sound"
63-
end
64-
end
65-
66-
# Child class
67-
class Dog < Animal
68-
def speak
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.
8026

8127
```ruby
8228
class BankAccount
8329
def initialize(balance)
8430
@balance = balance
8531
end
8632

87-
# Public method - can be called by anyone
88-
def display_balance
89-
"Current balance: $#{@balance}"
90-
end
91-
92-
# Protected method - can be called by other instances
93-
protected
94-
95-
def compare_balance(other)
96-
@balance > other.balance
33+
def deposit(amount)
34+
@balance += amount
9735
end
9836

99-
# Private method - can only be called internally
100-
private
101-
102-
def update_balance(amount)
103-
@balance += amount
37+
def current_balance
38+
@balance
10439
end
10540
end
10641

10742
account = BankAccount.new(100)
108-
puts account.display_balance
109-
# Output: Current balance: $100
43+
account.deposit(50)
44+
puts account.current_balance # Output: 150
11045
```
11146

112-
## Class Instance Variables
47+
## Attribute Accessors
11348

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:
11550

11651
```ruby
11752
class User
118-
# Create reader and writer methods
53+
# Creates both getter and setter methods
11954
attr_accessor :name
12055

121-
# Create reader only
56+
# Creates getter method only
12257
attr_reader :created_at
12358

124-
# Create writer only
59+
# Creates setter method only
12560
attr_writer :password
12661

12762
def initialize(name)
@@ -131,9 +66,36 @@ class User
13166
end
13267

13368
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+
class Animal
82+
def initialize(name)
83+
@name = name
84+
end
85+
86+
def speak
87+
"Some sound"
88+
end
89+
end
90+
91+
class Dog < Animal
92+
def speak
93+
"#{@name} says: Woof!"
94+
end
95+
end
96+
97+
dog = Dog.new("Rex")
98+
puts dog.speak # Output: Rex says: Woof!
13799
```
138100

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, reusable code by grouping related data and behavior into objects.

0 commit comments

Comments
 (0)