Skip to content

Commit 9eb14a3

Browse files
authored
Don't show 'Maybe IRB bug!' in show_source and ls command (#1039)
1 parent 0506ed0 commit 9eb14a3

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

lib/irb/command/ls.rb

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module IRB
1111

1212
module Command
1313
class Ls < Base
14-
include RubyArgsExtractor
14+
class EvaluationError < StandardError; end
1515

1616
category "Context"
1717
description "Show methods, constants, and variables."
@@ -22,24 +22,35 @@ class Ls < Base
2222
-g [query] Filter the output with a query.
2323
HELP_MESSAGE
2424

25+
def evaluate(code)
26+
@irb_context.workspace.binding.eval(code)
27+
rescue Exception => e
28+
puts "#{e.class}: #{e.message}"
29+
raise EvaluationError
30+
end
31+
2532
def execute(arg)
2633
if match = arg.match(/\A(?<target>.+\s|)(-g|-G)\s+(?<grep>.+)$/)
27-
if match[:target].empty?
28-
use_main = true
29-
else
30-
obj = @irb_context.workspace.binding.eval(match[:target])
31-
end
34+
target = match[:target]
3235
grep = Regexp.new(match[:grep])
36+
elsif match = arg.match(/\A((?<target>.+),|)\s*grep:(?<grep>.+)/)
37+
# Legacy style `ls obj, grep: /regexp/`
38+
# Evaluation order should be eval(target) then eval(grep)
39+
target = match[:target] || ''
40+
grep_regexp_code = match[:grep]
3341
else
34-
args, kwargs = ruby_args(arg)
35-
use_main = args.empty?
36-
obj = args.first
37-
grep = kwargs[:grep]
42+
target = arg.strip
3843
end
3944

40-
if use_main
45+
if target.empty?
4146
obj = irb_context.workspace.main
4247
locals = irb_context.workspace.binding.local_variables
48+
else
49+
obj = evaluate(target)
50+
end
51+
52+
if grep_regexp_code
53+
grep = evaluate(grep_regexp_code)
4354
end
4455

4556
o = Output.new(grep: grep)
@@ -52,6 +63,7 @@ def execute(arg)
5263
o.dump("class variables", klass.class_variables)
5364
o.dump("locals", locals) if locals
5465
o.print_result
66+
rescue EvaluationError
5567
end
5668

5769
def dump_methods(o, klass, obj)

lib/irb/source_finder.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,8 @@ def method_target(owner_receiver, super_level, method, type)
125125
end
126126

127127
def eval_receiver_or_owner(code)
128-
context_binding = @irb_context.workspace.binding
129-
eval(code, context_binding)
130-
rescue NameError
128+
@irb_context.workspace.binding.eval(code)
129+
rescue Exception
131130
raise EvaluationError
132131
end
133132

test/irb/command/test_show_source.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ def test_show_source_with_missing_constant
6565
assert_match(%r[Couldn't locate a definition for Foo], out)
6666
end
6767

68+
def test_show_source_with_eval_error
69+
write_ruby <<~'RUBY'
70+
binding.irb
71+
RUBY
72+
73+
out = run_ruby_file do
74+
type "show_source raise(Exception).itself"
75+
type "exit"
76+
end
77+
78+
assert_match(%r[Couldn't locate a definition for raise\(Exception\)\.itself], out)
79+
end
80+
6881
def test_show_source_string
6982
write_ruby <<~'RUBY'
7083
binding.irb

test/irb/test_command.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,19 @@ def test_ls_grep_empty
742742
end
743743
end
744744

745+
def test_ls_with_eval_error
746+
[
747+
"ls raise(Exception,'foo')\n",
748+
"ls raise(Exception,'foo'), grep: /./\n",
749+
"ls Integer, grep: raise(Exception,'foo')\n",
750+
].each do |line|
751+
out, err = execute_lines(line)
752+
assert_empty err
753+
assert_match(/Exception: foo/, out)
754+
assert_not_match(/Maybe IRB bug!/, out)
755+
end
756+
end
757+
745758
def test_ls_with_no_singleton_class
746759
out, err = execute_lines(
747760
"ls 42",

0 commit comments

Comments
 (0)