Skip to content

Commit 393ea3f

Browse files
committed
Add 'class' keyword
1 parent 44f3504 commit 393ea3f

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

lib/ruby_lsp/listeners/hover.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Hover
1010
Prism::BreakNode,
1111
Prism::CallNode,
1212
Prism::CaseNode,
13+
Prism::ClassNode,
1314
Prism::ConstantReadNode,
1415
Prism::ConstantWriteNode,
1516
Prism::ConstantPathNode,
@@ -58,6 +59,7 @@ def initialize(response_builder, global_state, uri, node_context, dispatcher, so
5859
self,
5960
:on_break_node_enter,
6061
:on_case_node_enter,
62+
:on_class_node_enter,
6163
:on_constant_read_node_enter,
6264
:on_constant_write_node_enter,
6365
:on_constant_path_node_enter,
@@ -111,6 +113,11 @@ def on_case_node_enter(node)
111113
handle_keyword_documentation(node.case_keyword)
112114
end
113115

116+
#: (Prism::ClassNode node) -> void
117+
def on_class_node_enter(node)
118+
handle_keyword_documentation(node.class_keyword)
119+
end
120+
114121
#: (Prism::InterpolatedStringNode node) -> void
115122
def on_interpolated_string_node_enter(node)
116123
generate_heredoc_hover(node)

lib/ruby_lsp/static_docs.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module RubyLsp
1616
KEYWORD_DOCS = {
1717
"break" => "Terminates the execution of a block, loop, or method",
1818
"case" => "Starts a case expression for pattern matching or multiple condition checking",
19+
"class" => "Defines a class and its methods",
1920
"yield" => "Invokes the passed block with the given arguments",
2021
}.freeze #: Hash[String, String]
2122
end

static_docs/class.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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

Comments
 (0)