Class: IRB::JobManager

Inherits:
Object show all
Defined in:
lib/irb/ext/multi-irb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJobManager

Creates a new JobManager object



18
19
20
21
# File 'lib/irb/ext/multi-irb.rb', line 18

def initialize
  @jobs = []
  @current_job = nil
end

Instance Attribute Details

#current_jobObject

The active irb session



24
25
26
# File 'lib/irb/ext/multi-irb.rb', line 24

def current_job
  @current_job
end

Instance Method Details

#delete(key) ⇒ Object

Deletes the job at the given key.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/irb/ext/multi-irb.rb', line 122

def delete(key)
  case key
  when Integer
    IRB.fail NoSuchJob, key unless @jobs[key]
    @jobs[key] = nil
  else
    catch(:EXISTS) do
      @jobs.each_index do
        |i|
        if @jobs[i] and (@jobs[i][0] == key ||
            @jobs[i][1] == key ||
            @jobs[i][1].context.main.equal?(key))
          @jobs[i] = nil
          throw :EXISTS
        end
      end
      IRB.fail NoSuchJob, key
    end
  end
  until assoc = @jobs.pop; end unless @jobs.empty?
  @jobs.push assoc
end

#insert(irb) ⇒ Object

Add the given irb session to the jobs Array.



57
58
59
# File 'lib/irb/ext/multi-irb.rb', line 57

def insert(irb)
  @jobs.push [Thread.current, irb]
end

#inspectObject

Outputs a list of jobs, see the irb command irb_jobs, or jobs.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/irb/ext/multi-irb.rb', line 146

def inspect
  ary = []
  @jobs.each_index do
    |i|
    th, irb = @jobs[i]
    next if th.nil?

    if th.alive?
      if th.stop?
        t_status = "stop"
      else
        t_status = "running"
      end
    else
      t_status = "exited"
    end
    ary.push format("#%d->%s on %s (%s: %s)",
      i,
      irb.context.irb_name,
      irb.context.main,
      th,
      t_status)
  end
  ary.join("\n")
end

#irb(key) ⇒ Object

Returns the irb session for the given key object, see #search for more information.



41
42
43
44
# File 'lib/irb/ext/multi-irb.rb', line 41

def irb(key)
  _, irb = search(key)
  irb
end

#kill(*keys) ⇒ Object

Terminates the irb sessions specified by the given keys.

Raises an IrbAlreadyDead exception if one of the given keys is already terminated.

See Thread#exit for more information.



84
85
86
87
88
89
90
# File 'lib/irb/ext/multi-irb.rb', line 84

def kill(*keys)
  for key in keys
    th, _ = search(key)
    IRB.fail IrbAlreadyDead unless th.alive?
    th.exit
  end
end

#main_irbObject

Returns the top level irb session.



52
53
54
# File 'lib/irb/ext/multi-irb.rb', line 52

def main_irb
  @jobs[0][1]
end

#main_threadObject

Returns the top level thread.



47
48
49
# File 'lib/irb/ext/multi-irb.rb', line 47

def main_thread
  @jobs[0][0]
end

#n_jobsObject

The total number of irb sessions, used to set irb_name of the current Context.



28
29
30
# File 'lib/irb/ext/multi-irb.rb', line 28

def n_jobs
  @jobs.size
end

#search(key) ⇒ Object

Returns the associated job for the given key.

If given an Integer, it will return the key index for the jobs Array.

When an instance of Irb is given, it will return the irb session associated with key.

If given an instance of Thread, it will return the associated thread key using Object#=== on the jobs Array.

Otherwise returns the irb session with the same top-level binding as the given key.

Raises a NoSuchJob exception if no job can be found with the given key.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/irb/ext/multi-irb.rb', line 106

def search(key)
  job = case key
        when Integer
          @jobs[key]
        when Irb
          @jobs.find{|k, v| v.equal?(key)}
        when Thread
          @jobs.assoc(key)
        else
          @jobs.find{|k, v| v.context.main.equal?(key)}
        end
  IRB.fail NoSuchJob, key if job.nil?
  job
end

#switch(key) ⇒ Object

Changes the current active irb session to the given key in the jobs Array.

Raises an IrbAlreadyDead exception if the given key is no longer alive.

If the given irb session is already active, an IrbSwitchedToCurrentThread exception is raised.



68
69
70
71
72
73
74
75
76
# File 'lib/irb/ext/multi-irb.rb', line 68

def switch(key)
  th, irb = search(key)
  IRB.fail IrbAlreadyDead unless th.alive?
  IRB.fail IrbSwitchedToCurrentThread if th == Thread.current
  @current_job = irb
  th.run
  Thread.stop
  @current_job = irb(Thread.current)
end

#thread(key) ⇒ Object

Returns the thread for the given key object, see #search for more information.



34
35
36
37
# File 'lib/irb/ext/multi-irb.rb', line 34

def thread(key)
  th, = search(key)
  th
end