Skip to content

Commit 383cd72

Browse files
committed
Add 'next' keyword
1 parent 847ba97 commit 383cd72

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

lib/ruby_lsp/listeners/hover.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Hover
3232
Prism::InstanceVariableTargetNode,
3333
Prism::InstanceVariableWriteNode,
3434
Prism::ModuleNode,
35+
Prism::NextNode,
3536
Prism::SymbolNode,
3637
Prism::StringNode,
3738
Prism::InterpolatedStringNode,
@@ -88,6 +89,7 @@ def initialize(response_builder, global_state, uri, node_context, dispatcher, so
8889
:on_instance_variable_or_write_node_enter,
8990
:on_instance_variable_target_node_enter,
9091
:on_module_node_enter,
92+
:on_next_node_enter,
9193
:on_super_node_enter,
9294
:on_forwarding_super_node_enter,
9395
:on_string_node_enter,
@@ -160,6 +162,11 @@ def on_module_node_enter(node)
160162
handle_keyword_documentation(node.module_keyword)
161163
end
162164

165+
#: (Prism::NextNode node) -> void
166+
def on_next_node_enter(node)
167+
handle_keyword_documentation(node.keyword)
168+
end
169+
163170
#: (Prism::InterpolatedStringNode node) -> void
164171
def on_interpolated_string_node_enter(node)
165172
generate_heredoc_hover(node)

lib/ruby_lsp/static_docs.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module RubyLsp
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",
2525
"module" => "Defines a module",
26+
"next" => "Skips to the next iteration of a loop",
2627
"yield" => "Invokes the passed block with the given arguments",
2728
}.freeze #: Hash[String, String]
2829
end

static_docs/next.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Next
2+
3+
In Ruby, the `next` keyword is used to skip the rest of the current iteration and move to the next iteration of a loop or block. It's similar to `continue` in other programming languages.
4+
5+
```ruby
6+
# Basic next usage in a loop
7+
["README.md", ".git", "lib", ".gitignore"].each do |path|
8+
next if path.start_with?(".")
9+
puts "Processing: #{path}"
10+
end
11+
# Output:
12+
# Processing: README.md
13+
# Processing: lib
14+
```
15+
16+
The `next` statement can be used with any of Ruby's iteration methods or blocks.
17+
18+
```ruby
19+
# Using next with different iterators
20+
users = [
21+
{ name: "Alice", active: true },
22+
{ name: "Bob", active: false },
23+
{ name: "Carol", active: true }
24+
]
25+
26+
# With each
27+
users.each do |user|
28+
next unless user[:active]
29+
puts "Notifying #{user[:name]}"
30+
end
31+
# Output:
32+
# Notifying Alice
33+
# Notifying Carol
34+
35+
# With map
36+
messages = users.map do |user|
37+
next "Account inactive" unless user[:active]
38+
"Welcome back, #{user[:name]}!"
39+
end
40+
puts messages.inspect
41+
# Output:
42+
# ["Welcome back, Alice!", "Account inactive", "Welcome back, Carol!"]
43+
```
44+
45+
## Conditional Next
46+
47+
The `next` keyword is often used with conditions to create more complex iteration logic.
48+
49+
```ruby
50+
# Processing specific elements
51+
orders = [
52+
{ id: 1, status: "paid" },
53+
{ id: 2, status: "pending" },
54+
{ id: 3, status: "cancelled" },
55+
{ id: 4, status: "paid" }
56+
]
57+
58+
orders.each do |order|
59+
# Skip non-paid orders
60+
next unless order[:status] == "paid"
61+
puts "Processing payment for order #{order[:id]}"
62+
end
63+
# Output:
64+
# Processing payment for order 1
65+
# Processing payment for order 4
66+
67+
# Processing with multiple conditions
68+
products = [
69+
{ name: "Book", price: 15, in_stock: true },
70+
{ name: "Shirt", price: 25, in_stock: false },
71+
{ name: "Hat", price: 12, in_stock: true }
72+
]
73+
74+
products.each do |product|
75+
next unless product[:in_stock] # Skip out of stock items
76+
next if product[:price] > 20 # Skip expensive items
77+
puts "Featured item: #{product[:name]} at $#{product[:price]}"
78+
end
79+
# Output:
80+
# Featured item: Book at $15
81+
# Featured item: Hat at $12
82+
```
83+
84+
The `next` keyword helps control the flow of iterations, allowing you to skip unwanted elements or conditions while continuing the loop.

0 commit comments

Comments
 (0)