ruby/lib/irb/ext/change-ws.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

39 lines
986 B
Ruby

# frozen_string_literal: true
#
# irb/ext/cb.rb -
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
module IRB # :nodoc:
class Context
# Inherited from +TOPLEVEL_BINDING+.
def home_workspace
if defined? @home_workspace
@home_workspace
else
@home_workspace = workspace
end
end
# Changes the current workspace to given object or binding.
#
# If the optional argument is omitted, the workspace will be
# #home_workspace which is inherited from +TOPLEVEL_BINDING+ or the main
# object, <code>IRB.conf[:MAIN_CONTEXT]</code> when irb was initialized.
#
# See IRB::WorkSpace.new for more information.
def change_workspace(*_main)
if _main.empty?
replace_workspace(home_workspace)
return main
end
replace_workspace(WorkSpace.new(_main[0]))
if !(class<<main;ancestors;end).include?(ExtendCommandBundle)
main.extend ExtendCommandBundle
end
end
end
end