Skip to content

Commit 847ba97

Browse files
committed
Add 'module' keyword
1 parent 7324157 commit 847ba97

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

lib/ruby_lsp/listeners/hover.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Hover
3131
Prism::InstanceVariableOrWriteNode,
3232
Prism::InstanceVariableTargetNode,
3333
Prism::InstanceVariableWriteNode,
34+
Prism::ModuleNode,
3435
Prism::SymbolNode,
3536
Prism::StringNode,
3637
Prism::InterpolatedStringNode,
@@ -86,6 +87,7 @@ def initialize(response_builder, global_state, uri, node_context, dispatcher, so
8687
:on_instance_variable_operator_write_node_enter,
8788
:on_instance_variable_or_write_node_enter,
8889
:on_instance_variable_target_node_enter,
90+
:on_module_node_enter,
8991
:on_super_node_enter,
9092
:on_forwarding_super_node_enter,
9193
:on_string_node_enter,
@@ -153,6 +155,11 @@ def on_for_node_enter(node)
153155
handle_keyword_documentation(node.for_keyword)
154156
end
155157

158+
#: (Prism::ModuleNode node) -> void
159+
def on_module_node_enter(node)
160+
handle_keyword_documentation(node.module_keyword)
161+
end
162+
156163
#: (Prism::InterpolatedStringNode node) -> void
157164
def on_interpolated_string_node_enter(node)
158165
generate_heredoc_hover(node)

lib/ruby_lsp/static_docs.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module RubyLsp
2222
"else" => "Executes the code in the else block if the condition is false",
2323
"ensure" => "Executes the code in the ensure block regardless of whether an exception is raised or not",
2424
"for" => "Iterates over a collection of elements",
25+
"module" => "Defines a module",
2526
"yield" => "Invokes the passed block with the given arguments",
2627
}.freeze #: Hash[String, String]
2728
end

static_docs/module.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Module
2+
3+
In Ruby, the `module` keyword creates a container for methods and constants. Modules serve two primary purposes: namespacing related code and providing reusable behavior through mixins.
4+
5+
```ruby
6+
# Basic module definition
7+
module Formatter
8+
def self.titleize(text)
9+
text.split.map(&:capitalize).join(" ")
10+
end
11+
end
12+
13+
puts Formatter.titleize("hello world")
14+
# Output:
15+
# Hello World
16+
```
17+
18+
Modules can be included in classes to share behavior through mixins, allowing for code reuse without inheritance.
19+
20+
```ruby
21+
# Module as a mixin
22+
module Printable
23+
def print_details
24+
puts "Name: #{name}"
25+
puts "ID: #{id}"
26+
end
27+
end
28+
29+
class Product
30+
include Printable
31+
attr_reader :name, :id
32+
33+
def initialize(name, id)
34+
@name = name
35+
@id = id
36+
end
37+
end
38+
39+
book = Product.new("Ruby Guide", "B123")
40+
book.print_details
41+
# Output:
42+
# Name: Ruby Guide
43+
# ID: B123
44+
```
45+
46+
## Namespacing
47+
48+
Modules help organize code by grouping related classes and methods under a namespace.
49+
50+
```ruby
51+
module Shop
52+
class Product
53+
def initialize(name)
54+
@name = name
55+
end
56+
end
57+
58+
class Order
59+
def initialize(product)
60+
@product = product
61+
end
62+
end
63+
end
64+
65+
# Using namespaced classes
66+
product = Shop::Product.new("Coffee")
67+
order = Shop::Order.new(product)
68+
```
69+
70+
## Multiple Includes
71+
72+
A class can include multiple modules to compose different behaviors.
73+
74+
```ruby
75+
module Validatable
76+
def valid?
77+
!name.nil? && !id.nil?
78+
end
79+
end
80+
81+
module Displayable
82+
def display
83+
"#{name} (#{id})"
84+
end
85+
end
86+
87+
class Item
88+
include Validatable
89+
include Displayable
90+
91+
attr_reader :name, :id
92+
93+
def initialize(name, id)
94+
@name = name
95+
@id = id
96+
end
97+
end
98+
99+
item = Item.new("Laptop", "L456")
100+
puts item.valid? # Output: true
101+
puts item.display # Output: Laptop (L456)
102+
```
103+
104+
The `module` keyword is essential for organizing code and implementing Ruby's version of multiple inheritance through mixins.

0 commit comments

Comments
 (0)