Skip to content

Commit 89d0d73

Browse files
committed
Add 'else' keyword
1 parent 415a8ff commit 89d0d73

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

lib/ruby_lsp/listeners/hover.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Hover
1616
Prism::ConstantPathNode,
1717
Prism::DefNode,
1818
Prism::DefinedNode,
19+
Prism::ElseNode,
1920
Prism::GlobalVariableAndWriteNode,
2021
Prism::GlobalVariableOperatorWriteNode,
2122
Prism::GlobalVariableOrWriteNode,
@@ -68,6 +69,7 @@ def initialize(response_builder, global_state, uri, node_context, dispatcher, so
6869
:on_call_node_enter,
6970
:on_def_node_enter,
7071
:on_defined_node_enter,
72+
:on_else_node_enter,
7173
:on_global_variable_and_write_node_enter,
7274
:on_global_variable_operator_write_node_enter,
7375
:on_global_variable_or_write_node_enter,
@@ -132,6 +134,11 @@ def on_defined_node_enter(node)
132134
handle_keyword_documentation(node.keyword)
133135
end
134136

137+
#: (Prism::ElseNode node) -> void
138+
def on_else_node_enter(node)
139+
handle_keyword_documentation(node.else_keyword)
140+
end
141+
135142
#: (Prism::InterpolatedStringNode node) -> void
136143
def on_interpolated_string_node_enter(node)
137144
generate_heredoc_hover(node)

lib/ruby_lsp/static_docs.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module RubyLsp
1919
"class" => "Defines a class and its methods",
2020
"def" => "Defines a method",
2121
"defined?" => "Checks if a constant or method is defined",
22+
"else" => "Executes the code in the else block if the condition is false",
2223
"yield" => "Invokes the passed block with the given arguments",
2324
}.freeze #: Hash[String, String]
2425
end

static_docs/else.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Else
2+
3+
In Ruby, the `else` keyword is used to define an alternative execution path in conditional statements. It works with `if`, `unless`, `case`, and `begin/rescue` blocks to handle cases when the primary conditions are not met.
4+
5+
```ruby
6+
# Basic else usage with if
7+
status = "error"
8+
9+
if status == "success"
10+
puts "Operation completed"
11+
else
12+
puts "Operation failed"
13+
end
14+
# Output:
15+
# Operation failed
16+
```
17+
18+
The `else` clause can be used with various conditional structures in Ruby.
19+
20+
```ruby
21+
# With unless
22+
temperature = 25
23+
24+
unless temperature < 20
25+
puts "It's warm"
26+
else
27+
puts "It's cool"
28+
end
29+
# Output:
30+
# It's warm
31+
32+
# With case statement
33+
grade = "B"
34+
35+
case grade
36+
when "A"
37+
puts "Excellent!"
38+
when "B"
39+
puts "Good job!"
40+
else
41+
puts "Keep working hard!"
42+
end
43+
# Output:
44+
# Good job!
45+
```
46+
47+
## Error Handling
48+
49+
The `else` keyword is commonly used with `begin/rescue` blocks for error handling.
50+
51+
```ruby
52+
begin
53+
result = 10 / 0
54+
rescue ZeroDivisionError
55+
puts "Cannot divide by zero"
56+
else
57+
# Executes only if no error was raised
58+
puts "Result: #{result}"
59+
ensure
60+
puts "Calculation attempted"
61+
end
62+
# Output:
63+
# Cannot divide by zero
64+
# Calculation attempted
65+
```
66+
67+
## Ternary Operator Alternative
68+
69+
For simple conditions, Ruby provides a ternary operator as a concise alternative to `if/else`.
70+
71+
```ruby
72+
def check_temperature(temp)
73+
temp >= 25 ? "It's hot" : "It's cool"
74+
end
75+
76+
puts check_temperature(30) # Output: It's hot
77+
puts check_temperature(20) # Output: It's cool
78+
79+
# Compared to if/else
80+
def check_temperature_verbose(temp)
81+
if temp >= 25
82+
"It's hot"
83+
else
84+
"It's cool"
85+
end
86+
end
87+
```
88+
89+
## Method Return Values
90+
91+
The `else` clause affects the return value in conditional expressions.
92+
93+
```ruby
94+
def process_number(num)
95+
if num.even?
96+
"Even number: #{num}"
97+
else
98+
"Odd number: #{num}"
99+
end
100+
end
101+
102+
puts process_number(42) # Output: Even number: 42
103+
puts process_number(37) # Output: Odd number: 37
104+
```
105+
106+
The `else` keyword is fundamental to control flow in Ruby, providing clear paths for alternate execution when conditions are not met.

0 commit comments

Comments
 (0)