Skip to content

Commit 7fb05dd

Browse files
committed
Add 'def' keyword
1 parent 393ea3f commit 7fb05dd

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

lib/ruby_lsp/listeners/hover.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Hover
1414
Prism::ConstantReadNode,
1515
Prism::ConstantWriteNode,
1616
Prism::ConstantPathNode,
17+
Prism::DefNode,
1718
Prism::GlobalVariableAndWriteNode,
1819
Prism::GlobalVariableOperatorWriteNode,
1920
Prism::GlobalVariableOrWriteNode,
@@ -64,6 +65,7 @@ def initialize(response_builder, global_state, uri, node_context, dispatcher, so
6465
:on_constant_write_node_enter,
6566
:on_constant_path_node_enter,
6667
:on_call_node_enter,
68+
:on_def_node_enter,
6769
:on_global_variable_and_write_node_enter,
6870
:on_global_variable_operator_write_node_enter,
6971
:on_global_variable_or_write_node_enter,
@@ -118,6 +120,11 @@ def on_class_node_enter(node)
118120
handle_keyword_documentation(node.class_keyword)
119121
end
120122

123+
#: (Prism::DefNode node) -> void
124+
def on_def_node_enter(node)
125+
handle_keyword_documentation(node.def_keyword)
126+
end
127+
121128
#: (Prism::InterpolatedStringNode node) -> void
122129
def on_interpolated_string_node_enter(node)
123130
generate_heredoc_hover(node)

lib/ruby_lsp/static_docs.rb

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

static_docs/def.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Def
2+
3+
In Ruby, the `def` keyword is used to define methods. Methods are reusable blocks of code that can accept parameters and return values. Every method implicitly returns the value of its last executed expression.
4+
5+
```ruby
6+
# Basic method definition
7+
def greet(name)
8+
"Hello, #{name}!"
9+
end
10+
11+
puts greet("Ruby")
12+
# Output:
13+
# Hello, Ruby!
14+
```
15+
16+
Methods can be defined with different types of parameters, including optional and keyword arguments.
17+
18+
```ruby
19+
# Method with optional parameter
20+
def calculate_total(amount, tax = 0.1)
21+
amount + (amount * tax)
22+
end
23+
24+
puts calculate_total(100) # Output: 110.0
25+
puts calculate_total(100, 0.2) # Output: 120.0
26+
27+
# Method with keyword arguments
28+
def create_user(name:, email:, role: "member")
29+
"#{name} (#{email}) - #{role}"
30+
end
31+
32+
puts create_user(name: "Alice", email: "[email protected]")
33+
# Output: Alice ([email protected]) - member
34+
```
35+
36+
## Method Return Values
37+
38+
Methods return the value of their last expression by default, but can use an explicit `return` statement to exit early.
39+
40+
```ruby
41+
def check_status(value)
42+
return "Invalid" if value < 0
43+
44+
if value > 100
45+
"Too high"
46+
else
47+
"OK"
48+
end
49+
end
50+
51+
puts check_status(-1) # Output: Invalid
52+
puts check_status(50) # Output: OK
53+
puts check_status(150) # Output: Too high
54+
```
55+
56+
## Instance and Class Methods
57+
58+
Methods can be defined at both the instance and class level.
59+
60+
```ruby
61+
class Timer
62+
# Instance method - called on instances
63+
def start
64+
@time = Time.now
65+
"Timer started"
66+
end
67+
68+
# Class method - called on the class itself
69+
def self.now
70+
Time.now.strftime("%H:%M:%S")
71+
end
72+
end
73+
74+
timer = Timer.new
75+
puts timer.start # Output: Timer started
76+
puts Timer.now # Output: 14:30:45
77+
```
78+
79+
## Method Visibility
80+
81+
Methods can have different visibility levels using `private`, `protected`, or `public` (default).
82+
83+
```ruby
84+
class BankAccount
85+
def initialize(balance)
86+
@balance = balance
87+
end
88+
89+
def withdraw(amount)
90+
return "Insufficient funds" unless sufficient_funds?(amount)
91+
92+
process_withdrawal(amount)
93+
"Withdrawn: $#{amount}"
94+
end
95+
96+
private
97+
98+
def sufficient_funds?(amount)
99+
@balance >= amount
100+
end
101+
102+
def process_withdrawal(amount)
103+
@balance -= amount
104+
end
105+
end
106+
107+
account = BankAccount.new(100)
108+
puts account.withdraw(50) # Output: Withdrawn: $50
109+
```
110+
111+
The `def` keyword is essential for organizing code into reusable, maintainable methods that form the building blocks of Ruby programs.

0 commit comments

Comments
 (0)