3 args.each { |sym| class_eval %{ def #{sym}?; @#{sym}; end } }
5 def bool_writer *args; attr_writer(*args); end
6 def bool_accessor *args
11 def defer_all_other_method_calls_to obj
12 class_eval %{ def method_missing meth, *a, &b; @#{obj}.send meth, *a, &b; end }
23 klass = klass.superclass
28 ## takes a value which it yields and then returns, so that code
31 ## x = expensive_operation
37 ## with(expensive_operation) { |x| log "got #{x}" }
39 ## i'm sure there's pithy comment i could make here about the
40 ## superiority of lisp, but fuck lisp.
41 def returning x; yield x; x; end
43 ## clone of java-style whole-method synchronization
44 ## assumes a @mutex variable
45 def synchronized *meth
48 alias unsynchronized_#{meth} #{meth}
50 @mutex.synchronize { unsynchronized_#{meth}(*a, &b) }
59 self.gsub(/([a-z])([A-Z0-9])/, '\1-\2').downcase
62 def find_all_positions x
75 self[0 .. 0].upcase + self[1 .. -1]
78 ## a very complicated regex found on teh internets to split on
79 ## commas, unless they occurr within double quotes.
81 split(/,\s*(?=(?:[^"]*"[^"]*")*(?![^"]*"))/)
88 cut = s[0 ... len].rindex(/\s/)
91 s = s[(cut + 1) .. -1]
100 def normalize_whitespace
101 gsub(/\t/, " ").gsub(/\r/, "")
116 def in? range; range.member? self; end
120 def num_digits base=10
121 return 1 if self == 0
122 1 + (Math.log(self) / Math.log(10)).floor
126 if self < 128 && self >= 0
136 Hash[*self.map { |k, v| [k, v] unless o.include? k }.compact.flatten_one_level]
139 def select_by_value v=true
140 select { |k, vv| vv == v }.map { |x| x.first }
147 each_with_index { |x, i| ret << yield(x, i) }
151 def sum; inject(0) { |x, y| x + y }; end
155 each { |x| ret[x] = yield(x) }
159 # like find, except returns the value of the block rather than the
163 find { |e| ret ||= yield(e) }
168 best, bestval = nil, nil
171 if bestval.nil? || val < bestval
172 best, bestval = e, val
180 def flatten_one_level
181 inject([]) { |a, e| a + e }
184 def to_h; Hash[*flatten]; end
185 def rest; self[1..-1]; end
187 def to_boolean_h; Hash[*map { |x| [x, true] }.flatten]; end
192 sprintf "%012d", self
199 self + (60 - min) * 60
203 def midnight # within a second
204 self - (hour * 60 * 60) - (min * 60) - sec
207 def is_the_same_day? other
208 (midnight - other.midnight).abs < 1
211 def is_the_day_before? other
212 other.midnight - midnight <= 24 * 60 * 60 + 1
215 def to_nice_distance_s from=Time.now
216 later_than = (self < from)
217 diff = (self.to_i - from.to_i).abs.to_f
223 ["week", 4.345], # heh heh
226 ].argfind do |unit, size|
229 elsif size.nil? || diff.round < size
230 "#{diff.round} #{unit}s"
243 TO_NICE_S_MAX_LEN = 9 # e.g. "Yest.10am"
244 def to_nice_s from=Time.now
247 elsif month != from.month
250 if is_the_same_day? from
252 elsif is_the_day_before? from
253 "Yest." + nearest_hour.strftime("%l%P")
261 ## simple singleton module. far less complete and insane than the ruby
262 ## standard library one, but automatically forwards methods calls and
263 ## allows for constructors that take arguments.
265 ## You must have #initialize call "self.class.i_am_the_instance self"
266 ## at some point or everything will fail horribly
269 def instance; @instance; end
270 def instantiated?; defined?(@instance) && !@instance.nil?; end
271 def method_missing meth, *a, &b
272 raise "no instance defined!" unless defined? @instance
273 @instance.send meth, *a, &b
275 def i_am_the_instance o
276 raise "there can be only one! (instance)" if defined? @instance
281 def self.included klass
282 klass.extend ClassMethods