Skip to content

Commit c844176

Browse files
authored
Improve how command calls' return value is handled (#972)
In #934, we changed command calls to return nil only. This PR improves the behaviour even further by: - Not echoing the `nil` returned by command calls - Not overriding previous return value stored in `_` with the `nil` from commands
1 parent 35de7da commit c844176

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

lib/irb/context.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,6 @@ def evaluate(statement, line_no) # :nodoc:
602602
set_last_value(result)
603603
when Statement::Command
604604
statement.command_class.execute(self, statement.arg)
605-
set_last_value(nil)
606605
end
607606

608607
nil

lib/irb/statement.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def is_assignment?
6868
end
6969

7070
def suppresses_echo?
71-
false
71+
true
7272
end
7373

7474
def should_be_handled_by_debugger?

test/irb/test_command.rb

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def test_measure
239239
)
240240

241241
assert_empty err
242-
assert_match(/\A(TIME is added\.\n=> nil\nprocessing time: .+\n=> 3\n=> nil\n=> 3\n){2}/, out)
242+
assert_match(/\A(TIME is added\.\nprocessing time: .+\n=> 3\n=> 3\n){2}/, out)
243243
assert_empty(c.class_variables)
244244
end
245245

@@ -266,7 +266,7 @@ def test_measure_keeps_previous_value
266266
)
267267

268268
assert_empty err
269-
assert_match(/\ATIME is added\.\n=> nil\nprocessing time: .+\n=> 3\nprocessing time: .+\n=> 3/, out)
269+
assert_match(/\ATIME is added\.\nprocessing time: .+\n=> 3\nprocessing time: .+\n=> 3/, out)
270270
assert_empty(c.class_variables)
271271
end
272272

@@ -291,7 +291,7 @@ def test_measure_enabled_by_rc
291291
)
292292

293293
assert_empty err
294-
assert_match(/\Aprocessing time: .+\n=> 3\n=> nil\n=> 3\n/, out)
294+
assert_match(/\Aprocessing time: .+\n=> 3\n=> 3\n/, out)
295295
end
296296

297297
def test_measure_enabled_by_rc_with_custom
@@ -321,7 +321,7 @@ def test_measure_enabled_by_rc_with_custom
321321
conf: conf,
322322
)
323323
assert_empty err
324-
assert_match(/\Acustom processing time: .+\n=> 3\n=> nil\n=> 3\n/, out)
324+
assert_match(/\Acustom processing time: .+\n=> 3\n=> 3\n/, out)
325325
end
326326

327327
def test_measure_with_custom
@@ -353,7 +353,7 @@ def test_measure_with_custom
353353
)
354354

355355
assert_empty err
356-
assert_match(/\A=> 3\nCUSTOM is added\.\n=> nil\ncustom processing time: .+\n=> 3\n=> nil\n=> 3\n/, out)
356+
assert_match(/\A=> 3\nCUSTOM is added\.\ncustom processing time: .+\n=> 3\n=> 3\n/, out)
357357
end
358358

359359
def test_measure_toggle
@@ -385,7 +385,7 @@ def test_measure_toggle
385385
)
386386

387387
assert_empty err
388-
assert_match(/\AFOO is added\.\n=> nil\nfoo\n=> 1\nBAR is added\.\n=> nil\nbar\nfoo\n=> 2\n=> nil\nbar\n=> 3\n=> nil\n=> 4\n/, out)
388+
assert_match(/\AFOO is added\.\nfoo\n=> 1\nBAR is added\.\nbar\nfoo\n=> 2\nbar\n=> 3\n=> 4\n/, out)
389389
end
390390

391391
def test_measure_with_proc_warning
@@ -410,7 +410,7 @@ def test_measure_with_proc_warning
410410
)
411411

412412
assert_match(/to add custom measure/, err)
413-
assert_match(/\A=> 3\n=> nil\n=> 3\n/, out)
413+
assert_match(/\A=> 3\n=> 3\n/, out)
414414
assert_empty(c.class_variables)
415415
end
416416
end
@@ -429,8 +429,7 @@ def test_irb_source
429429
/=> "bug17564"\n/,
430430
/=> "bug17564"\n/,
431431
/ => "hi"\n/,
432-
/ => nil\n/,
433-
/=> "hi"\n/,
432+
/ => "hi"\n/,
434433
], out)
435434
end
436435

@@ -457,8 +456,7 @@ def test_irb_load
457456
/=> "bug17564"\n/,
458457
/=> "bug17564"\n/,
459458
/ => "hi"\n/,
460-
/ => nil\n/,
461-
/=> "bug17564"\n/,
459+
/ => "bug17564"\n/,
462460
], out)
463461
end
464462

@@ -760,10 +758,7 @@ def test_ls_with_no_singleton_class
760758

761759
class ShowDocTest < CommandTestCase
762760
def test_show_doc
763-
out, err = execute_lines(
764-
"show_doc String#gsub\n",
765-
"\n",
766-
)
761+
out, err = execute_lines("show_doc String#gsub")
767762

768763
# the former is what we'd get without document content installed, like on CI
769764
# the latter is what we may get locally
@@ -776,15 +771,10 @@ def test_show_doc
776771
end
777772

778773
def test_show_doc_without_rdoc
779-
out, err = without_rdoc do
780-
execute_lines(
781-
"show_doc String#gsub\n",
782-
"\n",
783-
)
774+
_, err = without_rdoc do
775+
execute_lines("show_doc String#gsub")
784776
end
785777

786-
# if it fails to require rdoc, it only returns the command object
787-
assert_match(/=> nil\n/, out)
788778
assert_include(err, "Can't display document because `rdoc` is not installed.\n")
789779
ensure
790780
# this is the only way to reset the redefined method without coupling the test with its implementation

test/irb/test_irb.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ def test_underscore_stores_last_result
5656
assert_include output, "=> 12"
5757
end
5858

59+
def test_commands_dont_override_stored_last_result
60+
write_ruby <<~'RUBY'
61+
binding.irb
62+
RUBY
63+
64+
output = run_ruby_file do
65+
type "1 + 1"
66+
type "ls"
67+
type "_ + 10"
68+
type "exit!"
69+
end
70+
71+
assert_include output, "=> 12"
72+
end
73+
5974
def test_evaluate_with_encoding_error_without_lineno
6075
if RUBY_ENGINE == 'truffleruby'
6176
omit "Remove me after https://github.com/ruby/prism/issues/2129 is addressed and adopted in TruffleRuby"

0 commit comments

Comments
 (0)