Module: Utils::IRB::Shell

Includes:
FileUtils, Tins::Find
Included in:
Object
Defined in:
lib/utils/irb.rb

Defined Under Namespace

Classes: ConstantWrapper, MethodWrapper, WrapperBase

Constant Summary collapse

Infinity =

I like to define the infinite.

1.0 / 0

Instance Method Summary collapse

Instance Method Details

#capture_output(with_stderr = false) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/utils/irb.rb', line 236

def capture_output(with_stderr = false)
  require 'tempfile'
  begin
    old_stdout, $stdout = $stdout, Tempfile.new('irb')
    if with_stderr
      old_stderr, $stderr = $stderr, $stdout
    end
    yield
  ensure
    $stdout, temp = old_stdout, $stdout
    with_stderr and $stderr = old_stderr
  end
  temp.rewind
  temp.read
end

#editObject



345
346
347
# File 'lib/utils/irb.rb', line 345

def edit
  $editor.full?(:edit, self)
end

#irb_all_class_instance_methods(obj = self) ⇒ Object

Return all instance methods of obj’s class.



95
96
97
98
# File 'lib/utils/irb.rb', line 95

def irb_all_class_instance_methods(obj = self)
  methods = obj.class.instance_methods
  irb_wrap_methods obj, methods
end

#irb_all_instance_methods(modul = self) ⇒ Object

Return all instance methods defined in module modul.



108
109
110
111
# File 'lib/utils/irb.rb', line 108

def irb_all_instance_methods(modul = self)
  methods = modul.instance_methods
  irb_wrap_methods modul, methods, true
end

#irb_all_methods(obj = self) ⇒ Object

Return all methods of obj (including obj’s eigenmethods.)



121
122
123
124
# File 'lib/utils/irb.rb', line 121

def irb_all_methods(obj = self)
  methods = obj.methods
  irb_wrap_methods obj, methods
end

#irb_class_instance_methods(obj = self) ⇒ Object

Return instance methods of obj’s class without the inherited/mixed in methods.



102
103
104
105
# File 'lib/utils/irb.rb', line 102

def irb_class_instance_methods(obj = self)
  methods = obj.class.instance_methods(false)
  irb_wrap_methods obj, methods
end

#irb_connect(uri = nil) ⇒ Object

Connect to an irb server.



88
89
90
# File 'lib/utils/irb.rb', line 88

def irb_connect(uri = nil)
  Utils::IRB::Service.connect(uri)
end

#irb_constants(modul = self) ⇒ Object

Return all the constants defined in modul.



223
224
225
# File 'lib/utils/irb.rb', line 223

def irb_constants(modul = self)
  modul.constants.map { |c| ConstantWrapper.new(modul.const_get(c), c) }.sort
end

#irb_edit(*files) ⇒ Object



341
342
343
# File 'lib/utils/irb.rb', line 341

def irb_edit(*files)
  $editor.full?(:edit, *files)
end

#irb_eigen_methods(obj = self) ⇒ Object

Return all eigen methods of obj.



136
137
138
# File 'lib/utils/irb.rb', line 136

def irb_eigen_methods(obj = self)
  irb_wrap_methods obj, obj.methods(false)
end

#irb_instance_methods(modul = self) ⇒ Object

Return instance methods defined in module modul without the inherited/mixed in methods.



115
116
117
118
# File 'lib/utils/irb.rb', line 115

def irb_instance_methods(modul = self)
  methods = modul.instance_methods(false)
  irb_wrap_methods modul, methods, true
end

#irb_load!(*files) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/utils/irb.rb', line 313

def irb_load!(*files)
  files = files.map { |f| f.gsub(/(\.rb)?\Z/, '.rb') }
  loaded = {}
  for file in files
    catch :found do
      Find.find('.') do |f|
        File.directory?(f) and next
        md5_f = Utils::MD5.md5(f)
        if f.end_with?(file) and !loaded[md5_f]
          Kernel.load f
          loaded[md5_f] = true
          STDERR.puts "Loaded '#{f}'."
        end
      end
      Find.find('.') do |f|
        File.directory?(f) and next
        md5_f = Utils::MD5.md5(f)
        if f.end_with?(file) and !loaded[md5_f]
          Kernel.load f
          loaded[md5_f] = true
          STDERR.puts "Loaded '#{f}'."
        end
      end
    end
  end
  nil
end

#irb_methods(obj = self) ⇒ Object

Return instance methods of obj’s class without the inherited/mixed in methods, but including obj’s eigenmethods.



128
129
130
131
132
133
# File 'lib/utils/irb.rb', line 128

def irb_methods(obj = self)
  methods = obj.class.ancestors[1..-1].inject(obj.methods) do |all, a|
    all -= a.instance_methods
  end
  irb_wrap_methods obj, methods
end

#irb_open(url = nil, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/utils/irb.rb', line 65

def irb_open(url = nil, &block)
  case
  when url
    system 'open', url
  when block
    Tempfile.open('wb') do |t|
      t.write capture_output(&block)
      t.rewind
      system 'open', t.path
    end
  when url = receiver_unless_main(method(__method__))
    irb_open url
  else
    raise ArgumentError, 'need an url or block'
  end
end

#irb_read(filename, chunk_size = 8_192) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
# File 'lib/utils/irb.rb', line 301

def irb_read(filename, chunk_size = 8_192)
  if block_given?
    File.open(filename) do |file|
      until file.eof?
        yield file.read(chunk_size)
      end
    end
  else
    IO.read filename
  end
end

#irb_restartObject

Restart this irb.



61
62
63
# File 'lib/utils/irb.rb', line 61

def irb_restart
  exec $0
end

#irb_server(uri = nil) ⇒ Object

Start an irb server.



83
84
85
# File 'lib/utils/irb.rb', line 83

def irb_server(uri = nil)
  Utils::IRB::Service.start(uri) {}
end

#irb_subclasses(klass = self) ⇒ Object

Return all the subclasses of klass. TODO implement subclasses w/out rails



228
229
230
# File 'lib/utils/irb.rb', line 228

def irb_subclasses(klass = self)
  klass.subclasses.map { |c| ConstantWrapper.new(eval(c), c) }.sort
end

#irb_timeObject



261
262
263
264
265
266
267
# File 'lib/utils/irb.rb', line 261

def irb_time
  s = Time.now
  yield
  d = Time.now - s
  warn "Took %.3fs seconds." % d
  d
end

#irb_time_tapObject



269
270
271
272
273
# File 'lib/utils/irb.rb', line 269

def irb_time_tap
  r = nil
  irb_time { r = yield }
  r
end

#irb_time_watch(duration = 1) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/utils/irb.rb', line 275

def irb_time_watch(duration = 1)
  start = Time.now
  pre = nil
  loop do
    cur = [ yield ].flatten
    unless pre
      pre = cur.map(&:to_f)
      cur = [ yield ].flatten
    end
    expired = Time.now - start
    diffs = cur.zip(pre).map { |c, p| c - p }
    rates = diffs.map { |d| d / duration }
    warn "#{expired} #{cur.zip(rates, diffs).map(&:inspect) * ' '} # / per sec."
    pre = cur.map(&:to_f)
    sleep duration
  end
end

#irb_toggle_loggingObject



356
357
358
359
360
361
362
363
364
365
366
# File 'lib/utils/irb.rb', line 356

def irb_toggle_logging
  require 'logger'
  if ActiveRecord::Base.logger != $logger
    $old_logger = ActiveRecord::Base.logger
    ActiveRecord::Base.logger = $logger
    true
  else
    ActiveRecord::Base.logger = $old_logger
    false
  end
end

#irb_wrap_methods(obj = self, methods = methods(), modul = false) ⇒ Object



140
141
142
143
144
# File 'lib/utils/irb.rb', line 140

def irb_wrap_methods(obj = self, methods = methods(), modul = false)
  methods.map do |name|
    MethodWrapper.new(obj, name, modul) rescue nil
  end.compact.sort!
end

#irb_write(filename, text = nil, &block) ⇒ Object



293
294
295
296
297
298
299
# File 'lib/utils/irb.rb', line 293

def irb_write(filename, text = nil, &block)
  if text.nil? && block
    File.secure_write filename, nil, 'wb', &block
  else
    File.secure_write filename, text, 'wb'
  end
end

#less(with_stderr = false, &block) ⇒ Object

Use pager on the output of the commands given in the block.



253
254
255
256
257
258
259
# File 'lib/utils/irb.rb', line 253

def less(with_stderr = false, &block)
  IO.popen($pager, 'w') do |f|
    f.write capture_output(with_stderr, &block)
    f.close_write
  end
  nil
end

#ls(*args) ⇒ Object

List contents of directory



350
351
352
# File 'lib/utils/irb.rb', line 350

def ls(*args)
  puts `ls #{args.map { |x| "'#{x}'" } * ' '}`
end

#ri(*patterns, doc: 'ri') ⇒ Object

Start ri for pattern. If pattern is not string like, call it with pattern.class.name as argument.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/utils/irb.rb', line 38

def ri(*patterns, doc: 'ri')
  patterns.empty? and
    receiver_unless_main(method(__method__)) do |pattern|
    return ri(pattern, doc: doc)
  end
  patterns.map! { |p|
    case
    when Module === p
      p.name
    when p.respond_to?(:to_str)
      p.to_str
    else
      p.class.name
    end
  }
  system "#{doc} #{patterns.map { |p| "'#{p}'" } * ' ' } | #$pager"
end

#yri(*patterns) ⇒ Object



56
57
58
# File 'lib/utils/irb.rb', line 56

def yri(*patterns)
  ri *patterns, doc: 'yri'
end