2 ## only valid for integer ranges (unless I guess it's exclusive)
4 last - first + (exclude_end? ? 0 : 1)
10 args.each { |sym| class_eval %{ def #{sym}?; @#{sym}; end } }
12 def bool_writer *args; attr_writer(*args); end
13 def bool_accessor *args
18 def defer_all_other_method_calls_to obj
20 def method_missing meth, *a, &b; @#{obj}.send meth, *a, &b; end
21 def respond_to? meth; @#{obj}.respond_to?(meth); end
33 klass = klass.superclass
38 ## takes a value which it yields and then returns, so that code
41 ## x = expensive_operation
47 ## with(expensive_operation) { |x| log "got #{x}" }
49 ## i'm sure there's pithy comment i could make here about the
50 ## superiority of lisp, but fuck lisp.
51 def returning x; yield x; x; end
53 ## clone of java-style whole-method synchronization
54 ## assumes a @mutex variable
55 def synchronized *meth
58 alias unsynchronized_#{meth} #{meth}
60 @mutex.synchronize { unsynchronized_#{meth}(*a, &b) }
69 self.gsub(/([a-z])([A-Z0-9])/, '\1-\2').downcase
72 def find_all_positions x
85 self[0 .. 0].upcase + self[1 .. -1]
88 ## a very complicated regex found on teh internets to split on
89 ## commas, unless they occurr within double quotes.
91 split(/,\s*(?=(?:[^"]*"[^"]*")*(?![^"]*"))/)
98 cut = s[0 ... len].rindex(/\s/)
101 s = s[(cut + 1) .. -1]
110 def normalize_whitespace
111 gsub(/\t/, " ").gsub(/\r/, "")
126 def in? range; range.member? self; end
130 def num_digits base=10
131 return 1 if self == 0
132 1 + (Math.log(self) / Math.log(10)).floor
136 if self < 128 && self >= 0
146 Hash[*self.map { |k, v| [k, v] unless o.include? k }.compact.flatten_one_level]
149 def select_by_value v=true
150 select { |k, vv| vv == v }.map { |x| x.first }
157 each_with_index { |x, i| ret << yield(x, i) }
161 def sum; inject(0) { |x, y| x + y }; end
165 each { |x| ret[x] = yield(x) }
169 # like find, except returns the value of the block rather than the
173 find { |e| ret ||= yield(e) }
178 best, bestval = nil, nil
181 if bestval.nil? || val < bestval
182 best, bestval = e, val
190 def flatten_one_level
191 inject([]) { |a, e| a + e }
194 def to_h; Hash[*flatten]; end
195 def rest; self[1..-1]; end
197 def to_boolean_h; Hash[*map { |x| [x, true] }.flatten]; end
202 sprintf "%012d", self
209 self + (60 - min) * 60
213 def midnight # within a second
214 self - (hour * 60 * 60) - (min * 60) - sec
217 def is_the_same_day? other
218 (midnight - other.midnight).abs < 1
221 def is_the_day_before? other
222 other.midnight - midnight <= 24 * 60 * 60 + 1
225 def to_nice_distance_s from=Time.now
226 later_than = (self < from)
227 diff = (self.to_i - from.to_i).abs.to_f
233 ["week", 4.345], # heh heh
236 ].argfind do |unit, size|
239 elsif size.nil? || diff.round < size
240 "#{diff.round} #{unit}s"
253 TO_NICE_S_MAX_LEN = 9 # e.g. "Yest.10am"
254 def to_nice_s from=Time.now
257 elsif month != from.month
260 if is_the_same_day? from
262 elsif is_the_day_before? from
263 "Yest." + nearest_hour.strftime("%l%P")
271 ## simple singleton module. far less complete and insane than the ruby
272 ## standard library one, but automatically forwards methods calls and
273 ## allows for constructors that take arguments.
275 ## You must have #initialize call "self.class.i_am_the_instance self"
276 ## at some point or everything will fail horribly.
279 def instance; @instance; end
280 def instantiated?; defined?(@instance) && !@instance.nil?; end
281 def deinstantiate!; @instance = nil; end
282 def method_missing meth, *a, &b
283 raise "no instance defined!" unless defined? @instance
284 @instance.send meth, *a, &b
286 def i_am_the_instance o
287 raise "there can be only one! (instance)" if defined? @instance
292 def self.included klass
293 klass.extend ClassMethods
297 ## wraps an object. if it throws an exception, keeps a copy, and
298 ## rethrows it for any further method calls.
305 def clear_error!; @e = nil; end
306 def has_errors?; !@e.nil?; end
309 def method_missing m, *a, &b; __pass m, *a, &b; end
311 def id; __pass :id; end
312 def to_s; __pass :to_s; end
313 def to_yaml x; __pass :to_yaml, x; end
314 def is_a? c; @o.is_a? c; end
316 def respond_to? m; @o.respond_to? m end
321 rescue Exception => e
328 ## acts like a hash with an initialization block, but saves any
329 ## newly-created value even upon lookup.
334 ## attr_accessor :val
335 ## def initialize; @val = 0 end
338 ## h = Hash.new { C.new }
343 ## h2 = SavingHash.new { C.new }
348 ## important note: you REALLY want to use #member? to test existence,
349 ## because just checking h[anything] will always evaluate to true
350 ## (except for degenerate constructor blocks that return nil or false)
358 @hash[k] ||= @constructor.call(k)
361 defer_all_other_method_calls_to :hash