Skip to content

Commit

Permalink
Suppress command return values (#934)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
st0012 authored Apr 26, 2024
1 parent a91a212 commit fa96bea
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
5 changes: 3 additions & 2 deletions lib/irb/command/chws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CurrentWorkingWorkspace < Base
description "Show the current workspace."

def execute(_arg)
irb_context.main
puts "Current workspace: #{irb_context.main}"
end
end

Expand All @@ -30,7 +30,8 @@ def execute(arg)
obj = eval(arg, irb_context.workspace.binding)
irb_context.change_workspace(obj)
end
irb_context.main

puts "Current workspace: #{irb_context.main}"
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/irb/command/subirb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def execute_internal(*obj)

extend_irb_context
IRB.irb(nil, *obj)
puts IRB.JobManager.inspect
end
end

Expand All @@ -65,7 +66,7 @@ def execute(_arg)
end

extend_irb_context
IRB.JobManager
puts IRB.JobManager.inspect
end
end

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

raise CommandArgumentError.new("Please specify the id of target IRB job (listed in the `jobs` command).") unless key
IRB.JobManager.switch(key)
puts IRB.JobManager.inspect
end
end

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

extend_irb_context
IRB.JobManager.kill(*keys)
puts IRB.JobManager.inspect
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -587,18 +587,19 @@ def inspect_mode=(opt)

def evaluate(statement, line_no) # :nodoc:
@line_no = line_no
result = nil

case statement
when Statement::EmptyInput
return
when Statement::Expression
result = evaluate_expression(statement.code, line_no)
set_last_value(result)
when Statement::Command
result = statement.command_class.execute(self, statement.arg)
statement.command_class.execute(self, statement.arg)
set_last_value(nil)
end

set_last_value(result)
nil
end

def evaluate_expression(code, line_no) # :nodoc:
Expand Down
14 changes: 6 additions & 8 deletions test/irb/test_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,11 @@ class Foo; end
class CwwsTest < WorkspaceCommandTestCase
def test_cwws_returns_the_current_workspace_object
out, err = execute_lines(
"cwws",
"self.class"
"cwws"
)

assert_empty err
assert_include(out, self.class.name)
assert_include(out, "Current workspace: #{self}")
end
end

Expand Down Expand Up @@ -556,7 +555,7 @@ def test_popws_replaces_the_current_workspace_with_the_previous_one
"pushws Foo.new\n",
"popws\n",
"cwws\n",
"_.class",
"self.class",
)
assert_empty err
assert_include(out, "=> #{self.class}")
Expand All @@ -576,20 +575,19 @@ def test_chws_replaces_the_current_workspace
out, err = execute_lines(
"chws #{self.class}::Foo.new\n",
"cwws\n",
"_.class",
"self.class\n"
)
assert_empty err
assert_include(out, "Current workspace: #<#{self.class.name}::Foo")
assert_include(out, "=> #{self.class}::Foo")
end

def test_chws_does_nothing_when_receiving_no_argument
out, err = execute_lines(
"chws\n",
"cwws\n",
"_.class",
)
assert_empty err
assert_include(out, "=> #{self.class}")
assert_include(out, "Current workspace: #{self}")
end
end

Expand Down

0 comments on commit fa96bea

Please sign in to comment.