Skip to content

Commit fa96bea

Browse files
authored
Suppress command return values (#934)
Since commands can't be chained with methods, their return values are not intended to be used. But if IRB keeps storing command return values as the last value, and print them, users may rely on such implicit behaviour. So to avoid such confusion, this commit suppresses command's return values. It also updates some commands that currently rely on this implicit behaviour.
1 parent a91a212 commit fa96bea

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

lib/irb/command/chws.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CurrentWorkingWorkspace < Base
1515
description "Show the current workspace."
1616

1717
def execute(_arg)
18-
irb_context.main
18+
puts "Current workspace: #{irb_context.main}"
1919
end
2020
end
2121

@@ -30,7 +30,8 @@ def execute(arg)
3030
obj = eval(arg, irb_context.workspace.binding)
3131
irb_context.change_workspace(obj)
3232
end
33-
irb_context.main
33+
34+
puts "Current workspace: #{irb_context.main}"
3435
end
3536
end
3637
end

lib/irb/command/subirb.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def execute_internal(*obj)
4949

5050
extend_irb_context
5151
IRB.irb(nil, *obj)
52+
puts IRB.JobManager.inspect
5253
end
5354
end
5455

@@ -65,7 +66,7 @@ def execute(_arg)
6566
end
6667

6768
extend_irb_context
68-
IRB.JobManager
69+
puts IRB.JobManager.inspect
6970
end
7071
end
7172

@@ -90,6 +91,7 @@ def execute_internal(key = nil)
9091

9192
raise CommandArgumentError.new("Please specify the id of target IRB job (listed in the `jobs` command).") unless key
9293
IRB.JobManager.switch(key)
94+
puts IRB.JobManager.inspect
9395
end
9496
end
9597

@@ -112,6 +114,7 @@ def execute_internal(*keys)
112114

113115
extend_irb_context
114116
IRB.JobManager.kill(*keys)
117+
puts IRB.JobManager.inspect
115118
end
116119
end
117120
end

lib/irb/context.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,18 +587,19 @@ def inspect_mode=(opt)
587587

588588
def evaluate(statement, line_no) # :nodoc:
589589
@line_no = line_no
590-
result = nil
591590

592591
case statement
593592
when Statement::EmptyInput
594593
return
595594
when Statement::Expression
596595
result = evaluate_expression(statement.code, line_no)
596+
set_last_value(result)
597597
when Statement::Command
598-
result = statement.command_class.execute(self, statement.arg)
598+
statement.command_class.execute(self, statement.arg)
599+
set_last_value(nil)
599600
end
600601

601-
set_last_value(result)
602+
nil
602603
end
603604

604605
def evaluate_expression(code, line_no) # :nodoc:

test/irb/test_command.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,11 @@ class Foo; end
485485
class CwwsTest < WorkspaceCommandTestCase
486486
def test_cwws_returns_the_current_workspace_object
487487
out, err = execute_lines(
488-
"cwws",
489-
"self.class"
488+
"cwws"
490489
)
491490

492491
assert_empty err
493-
assert_include(out, self.class.name)
492+
assert_include(out, "Current workspace: #{self}")
494493
end
495494
end
496495

@@ -556,7 +555,7 @@ def test_popws_replaces_the_current_workspace_with_the_previous_one
556555
"pushws Foo.new\n",
557556
"popws\n",
558557
"cwws\n",
559-
"_.class",
558+
"self.class",
560559
)
561560
assert_empty err
562561
assert_include(out, "=> #{self.class}")
@@ -576,20 +575,19 @@ def test_chws_replaces_the_current_workspace
576575
out, err = execute_lines(
577576
"chws #{self.class}::Foo.new\n",
578577
"cwws\n",
579-
"_.class",
578+
"self.class\n"
580579
)
581580
assert_empty err
581+
assert_include(out, "Current workspace: #<#{self.class.name}::Foo")
582582
assert_include(out, "=> #{self.class}::Foo")
583583
end
584584

585585
def test_chws_does_nothing_when_receiving_no_argument
586586
out, err = execute_lines(
587587
"chws\n",
588-
"cwws\n",
589-
"_.class",
590588
)
591589
assert_empty err
592-
assert_include(out, "=> #{self.class}")
590+
assert_include(out, "Current workspace: #{self}")
593591
end
594592
end
595593

0 commit comments

Comments
 (0)