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/)
74 s = s[(cut + 1) .. -1]
83 def normalize_whitespace
84 gsub(/\t/, " ").gsub(/\r/, "")
99 def in? range; range.member? self; end
103 def num_digits base=10
104 return 1 if self == 0
105 1 + (Math.log(self) / Math.log(10)).floor
109 if self < 128 && self >= 0
119 Hash[*self.map { |k, v| [k, v] unless o.include? k }.compact.flatten_one_level]
122 def select_by_value v=true
123 select { |k, vv| vv == v }.map { |x| x.first }
130 each_with_index { |x, i| ret << yield(x, i) }
134 def sum; inject(0) { |x, y| x + y }; end
138 each { |x| ret[x] = yield(x) }
142 # like find, except returns the value of the block rather than the
146 find { |e| ret ||= yield(e) }
151 best, bestval = nil, nil
154 if bestval.nil? || val < bestval
155 best, bestval = e, val
163 def flatten_one_level
164 inject([]) { |a, e| a + e }
167 def to_h; Hash[*flatten]; end
168 def rest; self[1..-1]; end
170 def to_boolean_h; Hash[*map { |x| [x, true] }.flatten]; end
172 ## apparently uniq doesn't use ==. wtf.
173 def remove_successive_dupes
188 sprintf "%012d", self
195 self + (60 - min) * 60
199 def midnight # within a second
200 self - (hour * 60 * 60) - (min * 60) - sec
203 def is_the_same_day? other
204 (midnight - other.midnight).abs < 1
207 def is_the_day_before? other
208 other.midnight - midnight <= 24 * 60 * 60 + 1
211 def to_nice_distance_s from=Time.now
212 later_than = (self < from)
213 diff = (self.to_i - from.to_i).abs.to_f
219 ["week", 4], # heh heh
222 ].argfind do |unit, size|
225 elsif size.nil? || diff < size
228 diff = (diff / size.to_f).round
239 TO_NICE_S_MAX_LEN = 9 # e.g. "Yest.10am"
240 def to_nice_s from=Time.now
243 elsif month != from.month
246 if is_the_same_day? from
248 elsif is_the_day_before? from
249 "Yest." + nearest_hour.strftime("%l%P")
257 ## simple singleton module. far less complete and insane than the ruby
258 ## standard library one, but automatically forwards methods calls and
259 ## allows for constructors that take arguments.
261 ## You must have #initialize call "self.class.i_am_the_instance self"
262 ## at some point or everything will fail horribly
265 def instance; @instance; end
266 def instantiated?; defined?(@instance) && !@instance.nil?; end
267 def method_missing meth, *a, &b
268 raise "no instance defined!" unless defined? @instance
269 @instance.send meth, *a, &b
271 def i_am_the_instance o
272 raise "there can be only one! (instance)" if defined? @instance
277 def self.included klass
278 klass.extend ClassMethods