summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authortomoya ishida <[email protected]>2024-12-04 06:55:27 +0900
committergit <[email protected]>2024-12-03 21:55:33 +0000
commite539342f65e468fc7f926277eff61cb8b8a60622 (patch)
treef224d57b8e1c249f19d69db777046b0ac1078f98 /lib
parent3a90663776f94e4a617a4069d1d073cc1eca0526 (diff)
[ruby/irb] Don't show 'Maybe IRB bug!' in show_source and ls command
(https://github.com/ruby/irb/pull/1039) https://github.com/ruby/irb/commit/9eb14a3a0b
Diffstat (limited to 'lib')
-rw-r--r--lib/irb/command/ls.rb34
-rw-r--r--lib/irb/source_finder.rb5
2 files changed, 25 insertions, 14 deletions
diff --git a/lib/irb/command/ls.rb b/lib/irb/command/ls.rb
index cbd9998bc4..944efd7570 100644
--- a/lib/irb/command/ls.rb
+++ b/lib/irb/command/ls.rb
@@ -11,7 +11,7 @@ module IRB
module Command
class Ls < Base
- include RubyArgsExtractor
+ class EvaluationError < StandardError; end
category "Context"
description "Show methods, constants, and variables."
@@ -22,24 +22,35 @@ module IRB
-g [query] Filter the output with a query.
HELP_MESSAGE
+ def evaluate(code)
+ @irb_context.workspace.binding.eval(code)
+ rescue Exception => e
+ puts "#{e.class}: #{e.message}"
+ raise EvaluationError
+ end
+
def execute(arg)
if match = arg.match(/\A(?<target>.+\s|)(-g|-G)\s+(?<grep>.+)$/)
- if match[:target].empty?
- use_main = true
- else
- obj = @irb_context.workspace.binding.eval(match[:target])
- end
+ target = match[:target]
grep = Regexp.new(match[:grep])
+ elsif match = arg.match(/\A((?<target>.+),|)\s*grep:(?<grep>.+)/)
+ # Legacy style `ls obj, grep: /regexp/`
+ # Evaluation order should be eval(target) then eval(grep)
+ target = match[:target] || ''
+ grep_regexp_code = match[:grep]
else
- args, kwargs = ruby_args(arg)
- use_main = args.empty?
- obj = args.first
- grep = kwargs[:grep]
+ target = arg.strip
end
- if use_main
+ if target.empty?
obj = irb_context.workspace.main
locals = irb_context.workspace.binding.local_variables
+ else
+ obj = evaluate(target)
+ end
+
+ if grep_regexp_code
+ grep = evaluate(grep_regexp_code)
end
o = Output.new(grep: grep)
@@ -52,6 +63,7 @@ module IRB
o.dump("class variables", klass.class_variables)
o.dump("locals", locals) if locals
o.print_result
+ rescue EvaluationError
end
def dump_methods(o, klass, obj)
diff --git a/lib/irb/source_finder.rb b/lib/irb/source_finder.rb
index c515da5702..6e1e58069f 100644
--- a/lib/irb/source_finder.rb
+++ b/lib/irb/source_finder.rb
@@ -125,9 +125,8 @@ module IRB
end
def eval_receiver_or_owner(code)
- context_binding = @irb_context.workspace.binding
- eval(code, context_binding)
- rescue NameError
+ @irb_context.workspace.binding.eval(code)
+ rescue Exception
raise EvaluationError
end