Module: Utils::IRB::Shell
Defined Under Namespace
Classes: ConstantWrapper, MethodWrapper, WrapperBase
Constant Summary collapse
- Infinity =
I like to define the infinite.
1.0 / 0
Instance Method Summary collapse
- #capture_output(with_stderr = false) ⇒ Object
- #ed(*files) ⇒ Object
-
#irb_all_class_instance_methods(obj = self) ⇒ Object
Return all instance methods of obj’s class.
-
#irb_all_instance_methods(modul = self) ⇒ Object
Return all instance methods defined in module modul.
-
#irb_all_methods(obj = self) ⇒ Object
Return all methods of obj (including obj’s eigenmethods.).
-
#irb_class_instance_methods(obj = self) ⇒ Object
Return instance methods of obj’s class without the inherited/mixed in methods.
-
#irb_constants(modul = self) ⇒ Object
Return all the constants defined in
modul
. -
#irb_eigen_methods(obj = self) ⇒ Object
Return all eigen methods of obj.
-
#irb_instance_methods(modul = self) ⇒ Object
Return instance methods defined in module modul without the inherited/mixed in methods.
- #irb_load!(*files) ⇒ Object
-
#irb_methods(obj = self) ⇒ Object
Return instance methods of obj’s class without the inherited/mixed in methods, but including obj’s eigenmethods.
- #irb_open(url = nil, &block) ⇒ Object
- #irb_read(filename, chunk_size = 8_192) ⇒ Object
-
#irb_subclasses(klass = self) ⇒ Object
Return all the subclasses of
klass
. - #irb_time(n = 1) ⇒ Object
- #irb_time_tap(n = 1) ⇒ Object
- #irb_time_watch(duration = 1) ⇒ Object
- #irb_toggle_logging ⇒ Object
- #irb_wrap_methods(obj = self, methods = methods(), modul = false) ⇒ Object
- #irb_write(filename, text = nil, &block) ⇒ Object
- #le(with_stderr = false, &block) ⇒ Object
-
#less(with_stderr = false, &block) ⇒ Object
Use pager on the output of the commands given in the block.
-
#ri(*patterns, doc: 'ri') ⇒ Object
Start ri for
pattern
. - #yri(*patterns) ⇒ Object
Instance Method Details
#capture_output(with_stderr = false) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/utils/irb.rb', line 216 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 |
#ed(*files) ⇒ Object
336 337 338 339 340 341 342 |
# File 'lib/utils/irb.rb', line 336 def ed(*files) if files.empty? $editor.full?(:edit, self) else $editor.full?(:edit, *files) end end |
#irb_all_class_instance_methods(obj = self) ⇒ Object
Return all instance methods of obj’s class.
75 76 77 78 |
# File 'lib/utils/irb.rb', line 75 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.
88 89 90 91 |
# File 'lib/utils/irb.rb', line 88 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.)
101 102 103 104 |
# File 'lib/utils/irb.rb', line 101 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.
82 83 84 85 |
# File 'lib/utils/irb.rb', line 82 def irb_class_instance_methods(obj = self) methods = obj.class.instance_methods(false) irb_wrap_methods obj, methods end |
#irb_constants(modul = self) ⇒ Object
Return all the constants defined in modul
.
203 204 205 |
# File 'lib/utils/irb.rb', line 203 def irb_constants(modul = self) modul.constants.map { |c| ConstantWrapper.new(modul.const_get(c), c) }.sort end |
#irb_eigen_methods(obj = self) ⇒ Object
Return all eigen methods of obj.
116 117 118 |
# File 'lib/utils/irb.rb', line 116 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.
95 96 97 98 |
# File 'lib/utils/irb.rb', line 95 def irb_instance_methods(modul = self) methods = modul.instance_methods(false) irb_wrap_methods modul, methods, true end |
#irb_load!(*files) ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/utils/irb.rb', line 317 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 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.
108 109 110 111 112 113 |
# File 'lib/utils/irb.rb', line 108 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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/utils/irb.rb', line 55 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
305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/utils/irb.rb', line 305 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 File.read filename end end |
#irb_subclasses(klass = self) ⇒ Object
Return all the subclasses of klass
. TODO implement subclasses w/out rails
208 209 210 |
# File 'lib/utils/irb.rb', line 208 def irb_subclasses(klass = self) klass.subclasses.map { |c| ConstantWrapper.new(eval(c), c) }.sort end |
#irb_time(n = 1) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/utils/irb.rb', line 245 def irb_time(n = 1) s = Time.now n.times do yield end d = Time.now - s if n == 1 warn "Took %.3fs seconds." % d else warn "Took %.3fs seconds, %.3fs per call (avg)." % [ d, d / n ] end d end |
#irb_time_tap(n = 1) ⇒ Object
259 260 261 262 263 |
# File 'lib/utils/irb.rb', line 259 def irb_time_tap(n = 1) r = nil irb_time(n) { r = yield } r end |
#irb_time_watch(duration = 1) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/utils/irb.rb', line 265 def irb_time_watch(duration = 1) start = Time.now pre = nil avg = Hash.new 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 } durs = cur.zip(rates).each_with_index.map { |(c, r), i| if r < 0 x = c.to_f / -r a = avg[i].to_f a -= a / 2 a += x / 2 d = Tins::Duration.new(a) ds = d.to_s ds.singleton_class { define_method(:to_f) { d.to_f } } avg[i] = ds end avg[i] } warn "#{expired} #{cur.zip(diffs, rates, durs).map(&:inspect) * ' '} 𝝙 / per sec." pre = cur.map(&:to_f) sleep duration end end |
#irb_toggle_logging ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/utils/irb.rb', line 346 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
120 121 122 123 124 |
# File 'lib/utils/irb.rb', line 120 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
297 298 299 300 301 302 303 |
# File 'lib/utils/irb.rb', line 297 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 |
#le(with_stderr = false, &block) ⇒ Object
232 233 234 |
# File 'lib/utils/irb.rb', line 232 def le(with_stderr = false, &block) less(with_stderr) { block.call(self) } end |
#less(with_stderr = false, &block) ⇒ Object
Use pager on the output of the commands given in the block.
237 238 239 240 241 242 243 |
# File 'lib/utils/irb.rb', line 237 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 |
#ri(*patterns, doc: 'ri') ⇒ Object
Start ri for pattern
. If pattern
is not string like, call it with pattern.class.name as argument.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/utils/irb.rb', line 33 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
51 52 53 |
# File 'lib/utils/irb.rb', line 51 def yri(*patterns) ri(*patterns, doc: 'yri') end |