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
13 def method_missing meth, *a, &b; @#{obj}.send meth, *a, &b; end
14 def respond_to? meth; @#{obj}.respond_to?(meth); end
26 klass = klass.superclass
31 ## takes a value which it yields and then returns, so that code
34 ## x = expensive_operation
40 ## with(expensive_operation) { |x| log "got #{x}" }
42 ## i'm sure there's pithy comment i could make here about the
43 ## superiority of lisp, but fuck lisp.
44 def returning x; yield x; x; end
46 ## clone of java-style whole-method synchronization
47 ## assumes a @mutex variable
48 def synchronized *meth
51 alias unsynchronized_#{meth} #{meth}
53 @mutex.synchronize { unsynchronized_#{meth}(*a, &b) }
62 self.gsub(/([a-z])([A-Z0-9])/, '\1-\2').downcase
65 def find_all_positions x
78 self[0 .. 0].upcase + self[1 .. -1]
81 ## a very complicated regex found on teh internets to split on
82 ## commas, unless they occurr within double quotes.
84 split(/,\s*(?=(?:[^"]*"[^"]*")*(?![^"]*"))/)
91 cut = s[0 ... len].rindex(/\s/)
94 s = s[(cut + 1) .. -1]
103 def normalize_whitespace
104 gsub(/\t/, " ").gsub(/\r/, "")
119 def in? range; range.member? self; end
123 def num_digits base=10
124 return 1 if self == 0
125 1 + (Math.log(self) / Math.log(10)).floor
129 if self < 128 && self >= 0
139 Hash[*self.map { |k, v| [k, v] unless o.include? k }.compact.flatten_one_level]
142 def select_by_value v=true
143 select { |k, vv| vv == v }.map { |x| x.first }
150 each_with_index { |x, i| ret << yield(x, i) }
154 def sum; inject(0) { |x, y| x + y }; end
158 each { |x| ret[x] = yield(x) }
162 # like find, except returns the value of the block rather than the
166 find { |e| ret ||= yield(e) }
171 best, bestval = nil, nil
174 if bestval.nil? || val < bestval
175 best, bestval = e, val
183 def flatten_one_level
184 inject([]) { |a, e| a + e }
187 def to_h; Hash[*flatten]; end
188 def rest; self[1..-1]; end
190 def to_boolean_h; Hash[*map { |x| [x, true] }.flatten]; end
195 sprintf "%012d", self
202 self + (60 - min) * 60
206 def midnight # within a second
207 self - (hour * 60 * 60) - (min * 60) - sec
210 def is_the_same_day? other
211 (midnight - other.midnight).abs < 1
214 def is_the_day_before? other
215 other.midnight - midnight <= 24 * 60 * 60 + 1
218 def to_nice_distance_s from=Time.now
219 later_than = (self < from)
220 diff = (self.to_i - from.to_i).abs.to_f
226 ["week", 4.345], # heh heh
229 ].argfind do |unit, size|
232 elsif size.nil? || diff.round < size
233 "#{diff.round} #{unit}s"
246 TO_NICE_S_MAX_LEN = 9 # e.g. "Yest.10am"
247 def to_nice_s from=Time.now
250 elsif month != from.month
253 if is_the_same_day? from
255 elsif is_the_day_before? from
256 "Yest." + nearest_hour.strftime("%l%P")
264 ## simple singleton module. far less complete and insane than the ruby
265 ## standard library one, but automatically forwards methods calls and
266 ## allows for constructors that take arguments.
268 ## You must have #initialize call "self.class.i_am_the_instance self"
269 ## at some point or everything will fail horribly.
272 def instance; @instance; end
273 def instantiated?; defined?(@instance) && !@instance.nil?; end
274 def deinstantiate!; @instance = nil; end
275 def method_missing meth, *a, &b
276 raise "no instance defined!" unless defined? @instance
277 @instance.send meth, *a, &b
279 def i_am_the_instance o
280 raise "there can be only one! (instance)" if defined? @instance
285 def self.included klass
286 klass.extend ClassMethods
290 ## wraps an object. if it throws an exception, keeps a copy, and
291 ## rethrows it for any further method calls.
298 def clear_error!; @e = nil; end
299 def has_errors?; !@e.nil?; end
302 def method_missing m, *a, &b; __pass m, *a, &b; end
304 def id; __pass :id; end
305 def to_s; __pass :to_s; end
306 def to_yaml x; __pass :to_yaml, x; end
307 def is_a? c; @o.is_a? c; end
309 def respond_to? m; @o.respond_to? m end
314 rescue Exception => e
321 ## acts like a hash with an initialization block, but saves any
322 ## newly-created value even upon lookup.
327 ## attr_accessor :val
328 ## def initialize; @val = 0 end
331 ## h = Hash.new { C.new }
336 ## h2 = SavingHash.new { C.new }
347 @hash[k] ||= @constructor.call(k)
350 defer_all_other_method_calls_to :hash