-
Notifications
You must be signed in to change notification settings - Fork 2
/
irbrc
122 lines (115 loc) · 2.44 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
def mvim
edit_interactively 'mvim -f -c "set filetype=ruby"'
end
class Object
def send_chain(meths)
result = self
meths.each do |m|
return nil if result == nil
if result.respond_to?(m)
result = result.__send__(m)
elsif result.respond_to?(:[])
result = result[m]
else
raise "#{result.class} doesn't respond to #{m}"
end
end
result
end
end
module Enumerable
def supermap(*args, &block)
res = []
self.each do |e|
arr = []
args.each { |a| arr << e.send_chain(a.to_s.split('.')) }
arr << block.call(e) if block
res << arr
end
res
end
end
$vdiff_difftool = 'diff'
require 'pp'
class Object
def vdiff(other)
require 'tempfile'
t1 = Tempfile.new(['vdiff', '.rb'])
t2 = Tempfile.new(['vdiff', '.rb'])
PP.pp self, t1
PP.pp other, t2
t1.flush; t2.flush
puts `#{$vdiff_difftool} #{t1.path} #{t2.path}`
t1.close
t2.close
end
end
class Hash
def each_sorted_pair
self.keys.sort.each do |k|
yield k, self[k]
end
end
end
module PP::PPMethods
def pp_hash(obj)
group(1, '{', '}') {
seplist(obj, nil, :each_sorted_pair) {|k, v|
group {
pp k
text '=>'
group(1) {
breakable ''
pp v
}
}
}
}
end
end
# recursive diff that doesn't suck
require 'set'
class Array
def rdiff(other, seen = Set.new)
return if seen.include?(self)
seen << self
result = []
# this would be more awesome with FIBER
my = self
your = other
my.each_with_index do |elem, i|
if elem.respond_to?(:rdiff)
result << elem.rdiff(your, seen)
else
result << (elem == your[i] ? nil : your[i])
end
end
if my.size > your.size
(my.size - your.size).times { result << nil }
elsif your.size > my.size
(your.size - my.size).times { |i| result << your[my.size + i] }
end
result
end
end
class Hash
def rdiff(other_hash, seen = Set.new)
return if seen.include?(self)
seen << self
result = {}
(self.keys + other_hash.keys).uniq.each do |key|
my = self[key]
your = other_hash[key]
if my.respond_to?(:rdiff)
diff = my.rdiff(your, seen)
result[key] = diff if diff
else
result[key] = your unless my == your
end
end
result
end
end
def iglobal_id(id, shard_id = 1)
id + (10_000_000_000_000 * shard_id)
end