-
Notifications
You must be signed in to change notification settings - Fork 447
Support looking up global variables and predefined constants in ri #1477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adam12
wants to merge
1
commit into
ruby:master
Choose a base branch
from
adam12:ri-globals
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add the ability to look up documentation for global variables (e.g., $<, $LOAD_PATH) and predefined constants (e.g., STDIN, ARGV, RUBY_VERSION) directly through the ri command. Documentation is extracted from the globals.rdoc page in the system store. Changes: - Add display_global method to look up and display global documentation - Add extract_global_section to parse hierarchical headings in globals.rdoc - Add predefined_global_constant? to identify STDIN, ARGV, RUBY_* etc. - Update display_name to handle $-prefixed names and predefined constants - Update expand_name to skip expansion for globals - Fix nil matches bug in error handling when unknown globals are queried - Update help text with examples for global variable lookups
Contributor
Author
|
Parsing globals is less than ideal but I am not sure of a better approach. I'd love to see some support for keywords too but the same trick won't work (AFAICT). |
zenspider
approved these changes
Nov 27, 2025
Collaborator
|
🚀 Preview deployment available at: https://034ce9e8.rdoc-6cd.pages.dev (commit: 6c00877) |
Member
|
The PR currently shows diff --git a/lib/rdoc/ri/driver.rb b/lib/rdoc/ri/driver.rb
index fe95de0a..1b93516e 100644
--- a/lib/rdoc/ri/driver.rb
+++ b/lib/rdoc/ri/driver.rb
@@ -968,6 +968,16 @@ or the PAGER environment variable.
# Handle global variables immediately (classes can't start with $)
return display_global(name) if name.start_with?('$')
+ # Try predefined constants BEFORE class lookup to avoid case-insensitive
+ # filesystem matching (e.g., DATA matching Data class on macOS)
+ if predefined_global_constant?(name)
+ begin
+ return display_global(name)
+ rescue NotFoundError
+ # Fall through to class lookup
+ end
+ end
+
if name =~ /\w:(\w|$)/ then
display_page name
return true
@@ -977,21 +987,8 @@ or the PAGER environment variable.
display_method name if name =~ /::|#|\./
- # If no class was found and it's a predefined constant, try globals lookup
- # This handles ARGV, STDIN, etc. that look like class names but aren't
- return display_global(name) if predefined_global_constant?(name)
-
true
rescue NotFoundError
- # Before giving up, check if it's a predefined global constant
- if predefined_global_constant?(name)
- begin
- return display_global(name)
- rescue NotFoundError
- # Fall through to original error handling
- end
- end
-
matches = list_methods_matching name if name =~ /::|#|\./
matches = classes.keys.grep(/^#{Regexp.escape name}/) if matches.nil? || matches.empty?
diff --git a/test/rdoc/ri/driver_test.rb b/test/rdoc/ri/driver_test.rb
index 72f54b70..561207ba 100644
--- a/test/rdoc/ri/driver_test.rb
+++ b/test/rdoc/ri/driver_test.rb
@@ -1075,6 +1075,46 @@ Foo::Bar#bother
assert_match %r%command-line arguments%, out
end
+ def test_display_name_predefined_constant_over_class
+ util_store
+
+ # Create a Data class that could conflict with DATA constant
+ # (on case-insensitive filesystems, DATA could match Data)
+ @cData = @top_level.add_class RDoc::NormalClass, 'Data'
+ @cData.add_comment 'Data class for value objects', @top_level
+ @cData.record_location @top_level
+ @store1.save_class @cData
+
+ # Create a globals page with DATA constant
+ globals = @store1.add_file 'globals.rdoc'
+ globals.parser = RDoc::Parser::Simple
+ globals.comment = RDoc::Comment.from_document(doc(
+ head(1, 'Pre-Defined Global Constants'),
+ head(3, 'DATA'),
+ para('File object for lines after __END__.')
+ ))
+ @store1.save_page globals
+ @store1.type = :system
+
+ # DATA (all caps) should show the predefined constant, not the Data class
+ out, = capture_output do
+ @driver.display_name 'DATA'
+ end
+
+ assert_match %r%DATA%, out
+ assert_match %r%__END__%, out
+ refute_match %r%value objects%, out
+
+ # Data (capitalized) should show the class, not the constant
+ out, = capture_output do
+ @driver.display_name 'Data'
+ end
+
+ assert_match %r%Data%, out
+ assert_match %r%value objects%, out
+ refute_match %r%__END__%, out
+ end
+
def test_predefined_global_constant?
assert @driver.predefined_global_constant?('STDIN')
assert @driver.predefined_global_constant?('STDOUT') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add the ability to look up documentation for global variables (e.g., $<, $LOAD_PATH) and predefined constants (e.g., STDIN, ARGV, RUBY_VERSION) directly through the ri command. Documentation is extracted from the globals.rdoc page in the system store.
Changes: