3 args.each { |sym| class_eval %{ def #{sym}?; @#{sym}; end } }
5 def bool_writer *args; attr_writer(*args); end
6 def bool_accessor *args
19 klass = klass.superclass
24 ## takes a value which it yields and then returns, so that code
27 ## x = expensive_operation
33 ## with(expensive_operation) { |x| log "got #{x}" }
35 ## i'm sure there's pithy comment i could make here about the
36 ## superiority of lisp, but fuck lisp.
37 def with x; yield x; x; end
42 self.gsub(/([a-z])([A-Z0-9])/, '\1-\2').downcase
45 def find_all_positions x
58 self[0 .. 0].upcase + self[1 .. -1]
61 ## a very complicated regex found on teh internets to split on
62 ## commas, unless they occurr within double quotes.
64 split(/,\s*(?=(?:[^"]*"[^"]*")*(?![^"]*"))/)
71 cut = s[0 ... len].rindex(/\s/)
73 ret << s[0 ... cut] + "\n"
74 s = s[(cut + 1) .. -1]
76 ret << s[0 ... len] + "\n"
95 def in? range; range.member? self; end
99 def num_digits base=10
100 return 1 if self == 0
101 1 + (Math.log(self) / Math.log(10)).floor
105 if self < 128 && self >= 0
115 Hash[*self.map { |k, v| [k, v] unless o.include? k }.compact.flatten_one_level]
118 def select_by_value v=true
119 select { |k, vv| vv == v }.map { |x| x.first }
126 each_with_index { |x, i| ret << yield(x, i) }
130 def sum; inject(0) { |x, y| x + y }; end
134 each { |x| ret[x] = yield(x) }
138 # like find, except returns the value of the block rather than the
142 find { |e| ret ||= yield(e) }
147 best, bestval = nil, nil
150 if bestval.nil? || val < bestval
151 best, bestval = e, val
159 def flatten_one_level
160 inject([]) { |a, e| a + e }
163 def to_h; Hash[*flatten]; end
164 def rest; self[1..-1]; end
166 def to_boolean_h; Hash[*map { |x| [x, true] }.flatten]; end
168 ## apparently uniq doesn't use ==. wtf.
169 def remove_successive_dupes
184 sprintf "%012d", self
191 self + (60 - min) * 60
195 def midnight # within a second
196 self - (hour * 60 * 60) - (min * 60) - sec
199 def is_the_same_day? other
200 (midnight - other.midnight).abs < 1
203 def is_the_day_before? other
204 other.midnight - midnight <= 24 * 60 * 60 + 1
207 def to_nice_distance_s from=Time.now
208 later_than = (self < from)
209 diff = (self.to_i - from.to_i).abs.to_f
215 ["week", 4], # heh heh
218 ].argfind do |unit, size|
221 elsif size.nil? || diff < size
224 diff = (diff / size.to_f).round
235 TO_NICE_S_MAX_LEN = 9 # e.g. "Yest.10am"
236 def to_nice_s from=Time.now
239 elsif month != from.month
242 if is_the_same_day? from
244 elsif is_the_day_before? from
245 "Yest." + nearest_hour.strftime("%l%P")
253 ## simple singleton module. far less complete and insane than the ruby
254 ## standard library one, but automatically forwards methods calls and
255 ## allows for constructors that take arguments.
257 ## You must have #initialize call "self.class.i_am_the_instance self"
258 ## at some point or everything will fail horribly
261 def instance; @instance; end
262 def instantiated?; defined?(@instance) && !@instance.nil?; end
263 def method_missing meth, *a, &b
264 raise "no instance defined!" unless defined? @instance
265 @instance.send meth, *a, &b
267 def i_am_the_instance o
268 raise "there can be only one! (instance)" if defined? @instance
273 def self.included klass
274 klass.extend ClassMethods