ruby/lib/irb/ext/workspaces.rb
Stan Lo 57ca5960ad [ruby/irb] Restructure workspace management
(https://github.com/ruby/irb/pull/888)

* Remove dead irb_level method

* Restructure workspace management

Currently, workspace is an attribute of IRB::Context in most use cases.
But when some workspace commands are used, like `pushws` or `popws`, a
workspace will be created and used along side with the original workspace
attribute.

This complexity is not necessary and will prevent us from expanding
multi-workspace support in the future.

So this commit introduces a @workspace_stack ivar to IRB::Context so IRB
can have a more natural way to manage workspaces.

* Fix pushws without args

* Always display workspace stack after related commands are used

61560b99b3
2024-03-01 15:51:29 +00:00

37 lines
1.1 KiB
Ruby

# frozen_string_literal: true
#
# push-ws.rb -
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
module IRB # :nodoc:
class Context
# Creates a new workspace with the given object or binding, and appends it
# onto the current #workspaces stack.
#
# See IRB::Context#change_workspace and IRB::WorkSpace.new for more
# information.
def push_workspace(*_main)
if _main.empty?
if @workspace_stack.size > 1
# swap the top two workspaces
previous_workspace, current_workspace = @workspace_stack.pop(2)
@workspace_stack.push current_workspace, previous_workspace
end
else
@workspace_stack.push WorkSpace.new(workspace.binding, _main[0])
if !(class<<main;ancestors;end).include?(ExtendCommandBundle)
main.extend ExtendCommandBundle
end
end
end
# Removes the last element from the current #workspaces stack and returns
# it, or +nil+ if the current workspace stack is empty.
#
# Also, see #push_workspace.
def pop_workspace
@workspace_stack.pop if @workspace_stack.size > 1
end
end
end