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
-
#irb_all_class_instance_methods(obj) ⇒ Object
Return all instance methods of obj’s class.
-
#irb_all_instance_methods(modul) ⇒ Object
Return all instance methods defined in module modul.
-
#irb_all_methods(obj) ⇒ Object
Return all methods of obj (including obj’s eigenmethods.).
-
#irb_class_instance_methods(obj) ⇒ Object
Return instance methods of obj’s class without the inherited/mixed in methods.
- #irb_clipboard ⇒ Object
-
#irb_connect(hostname = nil, port = nil) ⇒ Object
Connect to an irb server.
-
#irb_constants(modul) ⇒ Object
Return all the constants defined in
modul
. - #irb_edit(*files) ⇒ Object
-
#irb_eigen_methods(obj) ⇒ Object
Return all eigen methods of obj.
-
#irb_fullinfo(obj) ⇒ Object
Output all the irb_info about
obj
. -
#irb_info(obj, detailed = nil) ⇒ Object
Output all kinds of information about
obj
. -
#irb_instance_methods(modul) ⇒ Object
Return instance methods defined in module modul without the inherited/mixed in methods.
- #irb_load!(*files) ⇒ Object
-
#irb_methods(obj) ⇒ Object
Return instance methods of obj’s class without the inherited/mixed in methods, but including obj’s eigenmethods.
- #irb_read(filename, chunk_size = 8_192) ⇒ Object
-
#irb_restart ⇒ Object
Restart this irb.
-
#irb_server(hostname = nil, port = nil) ⇒ Object
Start an irb server.
-
#irb_subclasses(klass) ⇒ Object
Return all the subclasses of
klass
. - #irb_time ⇒ Object
- #irb_time_tap ⇒ Object
- #irb_time_watch(duration = 1) ⇒ Object
- #irb_toggle_logging ⇒ Object
- #irb_wrap_methods(obj, methods, modul = false) ⇒ Object
- #irb_write(filename, text = nil) ⇒ Object
-
#less(with_stderr = false, &block) ⇒ Object
Use pager on the output of the commands given in the block.
-
#ls(*args) ⇒ Object
List contents of directory.
-
#ri(*patterns) ⇒ Object
Start ri for
pattern
.
Instance Method Details
#capture_output(with_stderr = false) ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/utils/irb.rb', line 264 def capture_output(with_stderr = false) return "missing block" unless block_given? 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 |
#irb_all_class_instance_methods(obj) ⇒ Object
Return all instance methods of obj’s class.
48 49 50 51 |
# File 'lib/utils/irb.rb', line 48 def irb_all_class_instance_methods(obj) methods = obj.class.instance_methods irb_wrap_methods obj, methods end |
#irb_all_instance_methods(modul) ⇒ Object
Return all instance methods defined in module modul.
61 62 63 64 |
# File 'lib/utils/irb.rb', line 61 def irb_all_instance_methods(modul) methods = modul.instance_methods irb_wrap_methods modul, methods, true end |
#irb_all_methods(obj) ⇒ Object
Return all methods of obj (including obj’s eigenmethods.)
74 75 76 77 |
# File 'lib/utils/irb.rb', line 74 def irb_all_methods(obj) methods = obj.methods irb_wrap_methods obj, methods end |
#irb_class_instance_methods(obj) ⇒ Object
Return instance methods of obj’s class without the inherited/mixed in methods.
55 56 57 58 |
# File 'lib/utils/irb.rb', line 55 def irb_class_instance_methods(obj) methods = obj.class.instance_methods(false) irb_wrap_methods obj, methods end |
#irb_clipboard ⇒ Object
99 100 101 102 103 104 |
# File 'lib/utils/irb.rb', line 99 def irb_clipboard case RUBY_PLATFORM when /darwin/ 'reattach-to-user-namespace pbcopy' end end |
#irb_connect(hostname = nil, port = nil) ⇒ Object
Connect to an irb server.
41 42 43 |
# File 'lib/utils/irb.rb', line 41 def irb_connect(hostname = nil, port = nil) Utils::IRB::Service.connect(hostname, port) end |
#irb_constants(modul) ⇒ Object
Return all the constants defined in modul
.
183 184 185 |
# File 'lib/utils/irb.rb', line 183 def irb_constants(modul) modul.constants.map { |c| ConstantWrapper.new(modul.const_get(c), c) }.sort end |
#irb_edit(*files) ⇒ Object
366 367 368 |
# File 'lib/utils/irb.rb', line 366 def irb_edit(*files) $editor.full?(:edit, *files) end |
#irb_eigen_methods(obj) ⇒ Object
Return all eigen methods of obj.
89 90 91 |
# File 'lib/utils/irb.rb', line 89 def irb_eigen_methods(obj) irb_wrap_methods obj, obj.methods(false) end |
#irb_fullinfo(obj) ⇒ Object
Output all the irb_info about obj
. You may need to buy a bigger screen for this or use:
less { irb_fullinfo object }
260 261 262 |
# File 'lib/utils/irb.rb', line 260 def irb_fullinfo(obj) irb_info obj, 0..Infinity end |
#irb_info(obj, detailed = nil) ⇒ Object
Output all kinds of information about obj
. If detailed is given output details about the methods (+ arity) in inheritance chain of obj
as well.
-
detailed as 0 output instance methods only of part 0 (the first) of the chain.
-
detailed as 1..2 output instance methods of
obj
inherited from parts 1 and 2 of the the chain.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/utils/irb.rb', line 202 def irb_info(obj, detailed = nil) if Module === obj modul = obj klassp = Class === modul if klassp begin allocated = modul.allocate rescue TypeError else obj = allocated end end else modul = obj.class end inspected = obj.inspect puts "obj = #{inspected.size > 40 ? inspected[0, 40] + '...' : inspected} is of class #{obj.class}." am = irb_all_methods(obj).size ms = irb_methods(obj).size ems = irb_eigen_methods(obj).size puts "obj: #{am} methods, #{ms} only local#{ems > 0 ? " (#{ems} eigenmethods),": ','} #{am - ms} inherited/mixed in." acim = irb_all_class_instance_methods(obj).size cim = irb_class_instance_methods(obj).size puts "obj: #{acim} instance methods, #{cim} local, #{acim - cim} only inherited/mixed in." if klassp s = modul.superclass puts "Superclass of #{modul}: #{s}" end a = [] ec = true begin a << (class << obj; self; end) rescue TypeError ec = false end a.concat modul.ancestors if ec puts "Ancestors of #{modul}: (#{a[0]},) #{a[1..-1].map { |k| "#{k}#{k == s ? '*' : ''}" } * ', '}" else puts "Ancestors of #{modul}: #{a[0..-1].map { |k| "#{k}#{k == s ? '*' : ''}" } * ', '}" end if Class === modul and detailed if detailed.respond_to? :to_int detailed = detailed..detailed end detailed.each do |i| break if i >= a.size k = a[i] puts "#{k}:" puts irb_wrap_methods(obj, k.instance_methods(false)).sort end end nil end |
#irb_instance_methods(modul) ⇒ Object
Return instance methods defined in module modul without the inherited/mixed in methods.
68 69 70 71 |
# File 'lib/utils/irb.rb', line 68 def irb_instance_methods(modul) methods = modul.instance_methods(false) irb_wrap_methods modul, methods, true end |
#irb_load!(*files) ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/utils/irb.rb', line 338 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) ⇒ Object
Return instance methods of obj’s class without the inherited/mixed in methods, but including obj’s eigenmethods.
81 82 83 84 85 86 |
# File 'lib/utils/irb.rb', line 81 def irb_methods(obj) methods = obj.class.ancestors[1..-1].inject(obj.methods) do |all, a| all -= a.instance_methods end irb_wrap_methods obj, methods end |
#irb_read(filename, chunk_size = 8_192) ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/utils/irb.rb', line 326 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_restart ⇒ Object
Restart this irb.
31 32 33 |
# File 'lib/utils/irb.rb', line 31 def irb_restart exec $0 end |
#irb_server(hostname = nil, port = nil) ⇒ Object
Start an irb server.
36 37 38 |
# File 'lib/utils/irb.rb', line 36 def irb_server(hostname = nil, port = nil) Utils::IRB::Service.start(hostname, port) {} end |
#irb_subclasses(klass) ⇒ Object
Return all the subclasses of klass
. TODO implement subclasses w/out rails
188 189 190 |
# File 'lib/utils/irb.rb', line 188 def irb_subclasses(klass) klass.subclasses.map { |c| ConstantWrapper.new(eval(c), c) }.sort end |
#irb_time ⇒ Object
290 291 292 293 294 295 296 |
# File 'lib/utils/irb.rb', line 290 def irb_time s = Time.now yield d = Time.now - s warn "Took %.3fs seconds." % d d end |
#irb_time_tap ⇒ Object
298 299 300 301 302 |
# File 'lib/utils/irb.rb', line 298 def irb_time_tap r = nil irb_time { r = yield } r end |
#irb_time_watch(duration = 1) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/utils/irb.rb', line 304 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_logging ⇒ Object
377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/utils/irb.rb', line 377 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, methods, modul = false) ⇒ Object
93 94 95 96 97 |
# File 'lib/utils/irb.rb', line 93 def irb_wrap_methods(obj, methods, modul = false) methods.map do |name| MethodWrapper.new(obj, name, modul) rescue nil end.compact.sort! end |
#irb_write(filename, text = nil) ⇒ Object
322 323 324 |
# File 'lib/utils/irb.rb', line 322 def irb_write(filename, text = nil) File.secure_write filename, text, 'wb' end |
#less(with_stderr = false, &block) ⇒ Object
Use pager on the output of the commands given in the block.
282 283 284 285 286 287 288 |
# File 'lib/utils/irb.rb', line 282 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
371 372 373 |
# File 'lib/utils/irb.rb', line 371 def ls(*args) puts `ls #{args.map { |x| "'#{x}'" } * ' '}` end |
#ri(*patterns) ⇒ Object
Start ri for pattern
. If pattern
is not string like, call it with pattern.class.name as argument.
25 26 27 28 |
# File 'lib/utils/irb.rb', line 25 def ri(*patterns) patterns.map! { |p| p.respond_to?(:to_str) ? p.to_str : p.class.name } system "ri #{patterns.map { |p| "'#{p}'" } * ' '} | #{$pager}" end |