-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.irbrc
460 lines (384 loc) · 11.9 KB
/
.irbrc
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
256
257
258
259
260
261
262
263
264
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# my.irbrc -- useful ruby methods for debugging and general use.
# Copyright (C)2010 Carnegie Mellon University
# Written by george.haff (@gmail)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'irb/completion'
require 'irb/ext/save-history'
require 'set'
asModule = false
begin
IRB.conf[:AUTO_INDENT] = true # automatically indents blocks when input spans lines
IRB.conf[:VERBOSE] = true # adds some amount of detail to certain output
IRB.conf[:SAVE_HISTORY] = 2000 # lines of history to save
rescue
# load as module by doing `ln -s ~/.irbrc ~/irbrc.rb`
# and then in your programs, include `require '~/irbrc'`
puts 'irbrc loaded as module'
asModule = true
end
# shortcut for rubydocs
def ri(*names)
system(%{ri #{names.map {|name| name.to_s}.join(" ")}})
end
# saves call stack and allows us to find callers for a method
$my_stack = []
set_trace_func proc { |event, file, line, id, binding, classname|
if event == "call" #|| event == "c-call"
$my_stack.push [classname, id]
elsif event == "return" #|| event == "c-return"
$my_stack.pop
end
}
def whoCalled()
# -1 is this method "whoCalled()",
# -2 is the caller who wants to know,
# -3 is the one who called our caller
$my_stack[-3]
end
def disableWhoCalled()
# beacause the trace_func seems to really slow down irb with deep stacks...
set_trace_func nil
end
if asModule
disableWhoCalled()
end
# for doing distributed work, with a limited number of concurrent jobs.
# 'job' blocks handed to this are responsible for their own locks
# and cleaning up any threads they spawn.
# also see Array.distribute() below
# how to use:
# distributor(700) {|i| sleep(rand(0)) }
#TODO: wrap yield calls in rescue block?, expand to ThreadGroup array instead of thread array?
def distributor(totalJobs, maxThreads=100)
toSpawn = [totalJobs, maxThreads].min
nextJobNo = 0
threads = []
while nextJobNo < totalJobs
##puts "spawning up to #{toSpawn} new jobs"
(nextJobNo..[nextJobNo+toSpawn, totalJobs].min-1).each {|t|
threads << Thread.new() {
##puts "starting #{t}"
yield(t)
##puts "finished #{t}"
}
}
nextJobNo += toSpawn
begin
sleep(10**-6) # sleep the minimum (one ruby time quantum)
threads = threads.select {|x| x.alive? }
toSpawn = maxThreads - threads.size
end until toSpawn > 0
end
##puts "done, cleaning up"
threads.each {|x| x.join }
end
# turns a collection of arrays of values into
# a hash from the collection of all values to a trivial hash (all keys present map to true) with the array indexes as keys
# arraysToIndicatorHash([:a,:b,:c], [:b,:c,:d], [:a,:c,:d])
# => { :a => { 0 => true, 2 => true },
# :b => { 0 => true, 1 => true },
# :c => { 0 => true, 1 => true, 2 => true },
# :d => { 1 => true, 2 => true }
# }
# TODO: turn trivial hashes into proper set objects
def arraysToIndicatorHash(*aa)
h = {}
aa.each_index{|i|
a = aa[i]
a.each{|x|
if not h.has_key?(x)
h[x] = {}
end
h[x][i] = true
}
}
h
end
# prints a hash of hashes as a matrix.
# assumes fixed width font but makes no assumptions about width.
# assumes all items in the data (or whatever they become after a .to_s call) are free of newlines.
# if using the sort block functionality, dont forget to make sure your comparitor handles nils
# (or whatever default value for your hashes) gracefully!
# puts prettyPrintAttrs(arraysToIndicatorHash([:a,:b,:c], [:b,:c,:d], [:a,:c,:d]))
#=> index 0 2 1
# =======================
# a true true -
# b true - true
# c true true true
# d - true true
#
# h = { :a=>{:i=>1, :v=>1, :x=>:b},
# :b=>{:i=>3, :v=>2, :x=>:b},
# :c=>{:i=>2, :v=>3},
# :d=>{:i=>5, :v=>4, :x=>:a}
# }
# puts prettyPrintAttrs(h, nil, 's', 3, '-', true){|a,b| a[:i]<=>b[:i] }
#=> s a c b d
# i 1 2 3 5
# v 1 3 2 4
# x b - b a
def prettyPrintAttrs(attrs, headerKeys=nil, keyHeaderName="index", colSpacing=2, defaultBlank="-", rotate=false)
if headerKeys.nil?
ks = {}
attrs.each_value { |v|
v.keys.each { |k|
ks[k] = (ks[k]||0)+1
}
}
headerKeys = ks.keys.sort{|a,b| ks[b]<=>ks[a] } # puts sparse columns at the end
end
cols = [[keyHeaderName]]
headerKeys.each{|k| cols<<[k.to_s] }
keys = attrs.keys
if block_given?
keys.sort!{|a,b| yield(attrs[a], attrs[b]) }
end
keys.each { |k|
v = attrs[k]
cols[0] << k.to_s
headerKeys.each_index { |i|
cols[i+1] << if v.has_key?(headerKeys[i])
v[headerKeys[i]].to_s
else
defaultBlank
end
}
}
if rotate
cols = cols.transpose
end
sizes = []
cols.each { |col|
sizes << col.max{|a,b| a.length<=>b.length }.length
}
ret = ""
cols[0].each_index { |i|
if i==1 #TODO:?? and !rotate
ret += "="*(sizes.inject{|sum,n| sum+n }+colSpacing*(sizes.length-1)) + "\n"
end
cols.each_index { |j|
ret += cols[j][i].ljust(sizes[j]+colSpacing)
}
ret += "\n"
}
ret
end
# has problems if the keys of the hash are hashes themselves,
# or if a particular object's .to_s is ugly
# example usage: puts hashPrint({:a => {:b => :c, :d => :e}, :f => :g})
# todo: do s/\n/prefix\n/ on any .to_s calls?
def hashPrint(h, prefix="")
spacer = " "
if h.class == Hash
"{\n" + h.inject(""){ |o, (k, v)|
o + prefix + spacer + k.inspect + " => " + hashPrint(v, prefix+spacer) #TODO: k.inspect WAS k.to_s
} + prefix + "}\n"
else #TODO: add handling of Array here?
h.inspect + "\n" #TODO: h.inspect WAS h.to_s
end
end
def hashPrintSorted(h, depth=0, sortFn=lambda{|x,y|lex(x,y)})
if h.class == Hash
"{\n" + h.keys.sort{|x,y|sortFn.call(x,y)}.inject(""){ |o, k|
o + " "*(depth+1) + k.to_s + " => " + hashPrintSorted(h[k], depth+1, sortFn)
} + " "*depth + "}\n"
else
h.to_s + "\n"
end
end
class Hash
def to_s
hashPrint(self)
end
def inspect
hashPrint(self)
end
# naive way of finding key and value types for a hash
def self.mapType(map)
k = map.keys[0]
return k.class.to_s+" --> "+map[k].class.to_s
end
def mapType(map=self)
Hash.mapType(map)
end
# redefines values of the hash, block provided should return new value to replace the 'v' it is given
def remap
self.merge(self){|k,v| yield v} #TODO???: yield k,v instead?
end
def remap!
self.merge!(self){|k,v| yield v} #TODO???: yield k,v instead?
end
end
class Integer
# use like .times(), except that this will stop when the block returns true.
# that is, it will execute the block at most the specified number of times
# (in cases where the block continually returns falsey values or throws
# exceptions), ultimately returning the value of the last iteration or
# rethrowing the last exception -- or it stops and returns the result of
# the first iteration that returns a truthy value.
# ...really, its for doing retries.
def tries
iteration = 0
success = false
while iteration < self and not success
iteration += 1
begin
success = yield iteration
rescue Exception => err
if iteration == self
raise err
end
end
end
return success
end
end
module Enumerable
# like Enumerable::select{}.size, but presumably doesnt use as much space
def count
self.inject(0){|total, o| total + (yield(o) ? 1 : 0)}
end
end
class Object
# add to these:
# whether a method takes a block?
# public||private class||instance members||methods
# ( private_instance_methods, protected_instance_methods, public_instance_methods,
# singleton_methods, constants - superclass.constants )
# detailed object information
def introspect(obj = self)
klass = obj.class
heirarchy = klass.name
superklass = klass.superclass
modules = klass.ancestors.to_set.delete(klass)
while not superklass.nil?
modules.delete(superklass)
heirarchy += " < " + superklass.name
superklass = superklass.superclass
end
puts "type:\n " + heirarchy + "\n"
puts "including modules:\n " + modules.to_a.join(", ") + "\n"
methodExtractor = Regexp.new("#<Method: (#{klass.name}(.*?)?#)?(.*?)>")
puts "having methods:\n" + obj.methods.map { |methodString|
methodRef = obj.method(methodString.intern)
definedIn, name = methodExtractor.match(methodRef.inspect)[2, 3]
if definedIn.nil? || definedIn.empty?
definedIn = ""
else
definedIn += " "
end
" " + definedIn + name + " :" + methodRef.arity.to_s
}.sort.join("\n") + "\n"
puts "inspection:\n " + obj.inspect + "\n"
end
# detailed object information in hash format
def introspectHash(obj = self)
klass = obj.class
klassname = klass.name
info = {}
modules = klass.ancestors.to_set
info[:inspect] = obj.inspect
info[:typeHeirarchy] = []
info[:methods] = {}
while not klass.nil?
info[:typeHeirarchy] << klass.name
modules.delete(klass)
klass = klass.superclass
end
info[:modules] = modules.to_a
methodExtractor = Regexp.new("#<Method: (#{klassname}(\\((.*?)\\))?#)?(.*?)>")
obj.methods.each { |methodString|
methodRef = obj.method(methodString.intern)
definedIn, name = methodExtractor.match(methodRef.inspect)[3, 4]
if definedIn.nil? || definedIn.empty?
definedIn = klassname
end
info[:methods][name] = { :arity => methodRef.arity,
:definedIn => definedIn }
}
return info
end
end
# returns array with all combinations of incident arrays:
# ["text/","application/"]**["x-yaml","yaml"]
# => ["text/x-yaml", "text/yaml", "application/x-yaml", "application/yaml"]
class Array
def **(a)
self.inject([]) { |m, x|
a.inject(m) { |n, y|
n << x.to_s+y.to_s
}
}
end
# extends distributor() from above...
# example use: doing some archiving job on a list of websites, with (up to) 3 active at a time
# ['www.google.com', 'www.cnn.com', 'www.cmu.edu', 'www.slashdot.org', 'bash.org'].distribute(3) {|site, arrayIndex|
# puts "polling site #{site}"
# mirrorAndSave(site) #or... sleep(rand(7))
# puts "done with #{site}"
# }
def distribute(maxThreads=100)
distributor(self.size, maxThreads) {|i| yield(self[i], i) }
self
end
end
class String
def startsWith(prefix)
String.startsWith(prefix, self)
end
def self.startsWith(prefix, test)
if prefix.kind_of?(Array)
prefix.any?{|p| String.startsWith(p, test)}
elsif test.kind_of?(Array)
test.any?{|t| String.startsWith(prefix, t)}
else
#TODO: would a regex be better here?
prefix.eql?(test[0, prefix.length])
end
end
end
def cmp(x, y)
x <=> y
end
def lex(x, y)
cmp(x.to_s, y.to_s)
end
def identity(i)
i
end
#use thusly:
# a,b = order(1,2)
# x,y = order(4,3)
# puts [a,b,x,y].inspect
#=> [1, 2, 3, 4]
def order(a, b)
if b<a
[b, a]
else
[a, b]
end
end
# for backwards compatibility, .tap() is standard in ruby 1.9, but not before
if not defined? tap
class Object
def tap
yield self
self
end
end
end
# some of the methods here that are attached to classes have an
# optional argument that defaults to self. this is from the first
# iteration of the methods that stood alone.