summaryrefslogtreecommitdiff
path: root/lib/irb/ext/workspaces.rb
blob: 5a1edd89b736e9fc3b01f99babd622d24bb8b80a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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