Skip to content

Commit f602fe1

Browse files
vinistockMikey Gough
authored andcommitted
Update Ruby version to v3.4.4 (#3541)
1 parent b291109 commit f602fe1

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.4.3
1+
3.4.4

lib/ruby_lsp/listeners/hover.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Hover
2929
Prism::SuperNode,
3030
Prism::ForwardingSuperNode,
3131
Prism::YieldNode,
32+
Prism::CaseNode,
3233
Prism::ClassVariableAndWriteNode,
3334
Prism::ClassVariableOperatorWriteNode,
3435
Prism::ClassVariableOrWriteNode,
@@ -75,6 +76,7 @@ def initialize(response_builder, global_state, uri, node_context, dispatcher, so
7576
:on_string_node_enter,
7677
:on_interpolated_string_node_enter,
7778
:on_yield_node_enter,
79+
:on_case_node_enter,
7880
:on_class_variable_and_write_node_enter,
7981
:on_class_variable_operator_write_node_enter,
8082
:on_class_variable_or_write_node_enter,
@@ -214,6 +216,11 @@ def on_yield_node_enter(node)
214216
handle_keyword_documentation(node.keyword)
215217
end
216218

219+
#: (Prism::CaseNode node) -> void
220+
def on_case_node_enter(node)
221+
handle_keyword_documentation(node.case_keyword)
222+
end
223+
217224
#: (Prism::ClassVariableAndWriteNode node) -> void
218225
def on_class_variable_and_write_node_enter(node)
219226
handle_class_variable_hover(node.name.to_s)

lib/ruby_lsp/static_docs.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ module RubyLsp
1515
# A map of keyword => short documentation to be displayed on hover or completion
1616
KEYWORD_DOCS = {
1717
"yield" => "Invokes the passed block with the given arguments",
18+
"case" => "Starts a case expression for pattern matching or multiple condition checking",
1819
}.freeze #: Hash[String, String]
1920
end

static_docs/case.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Case Statement
2+
3+
In Ruby, the `case` statement provides a clean way to express conditional logic when you need to compare a value against multiple conditions. It's similar to if/else chains but often more readable and concise.
4+
5+
```ruby
6+
# Basic case statement comparing a value against multiple conditions
7+
grade = "A"
8+
9+
case grade
10+
when "A"
11+
puts "Excellent!"
12+
when "B"
13+
puts "Good job!"
14+
when "C"
15+
puts "Fair"
16+
else
17+
puts "Need improvement"
18+
end
19+
```
20+
21+
The `case` statement can also work with ranges, multiple values, and even custom matching using the `===` operator.
22+
23+
```ruby
24+
# Case statement with ranges and multiple conditions
25+
score = 85
26+
27+
case score
28+
when 90..100
29+
puts "A grade"
30+
when 80..89
31+
puts "B grade"
32+
when 70..79
33+
puts "C grade"
34+
else
35+
puts "Need improvement"
36+
end
37+
38+
# Case with multiple values in a single when clause
39+
day = "Saturday"
40+
41+
case day
42+
when "Saturday", "Sunday"
43+
puts "Weekend!"
44+
else
45+
puts "Weekday"
46+
end
47+
```
48+
49+
## Pattern Matching (Ruby 2.7+)
50+
51+
Starting from Ruby 2.7, `case` statements support pattern matching, which provides powerful ways to match and destructure data.
52+
53+
```ruby
54+
# Pattern matching with arrays
55+
data = [1, 2, 3]
56+
57+
case data
58+
when [1, 2, 3]
59+
puts "Exact match!"
60+
when [1, *rest]
61+
puts "Starts with 1, followed by #{rest}"
62+
when Array
63+
puts "Any array"
64+
else
65+
puts "Not an array"
66+
end
67+
68+
# Pattern matching with hashes (Ruby 3.0+)
69+
user = { name: "Alice", age: 30 }
70+
71+
case user
72+
in { name: "Alice", age: }
73+
puts "Alice is #{age} years old"
74+
in { name:, age: 20.. }
75+
puts "#{name} is at least 20"
76+
else
77+
puts "No match"
78+
end
79+
```
80+
81+
## Case without an argument
82+
83+
Ruby also allows `case` statements without an explicit argument, which acts like a series of if/elsif conditions.
84+
85+
```ruby
86+
# Case statement without an argument
87+
case
88+
when Time.now.saturday?
89+
puts "It's Saturday!"
90+
when Time.now.sunday?
91+
puts "It's Sunday!"
92+
else
93+
puts "It's a weekday"
94+
end
95+
```
96+
97+
The case statement is particularly useful when you have multiple conditions to check against a single value, or when you want to use pattern matching to destructure complex data structures.

0 commit comments

Comments
 (0)