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
27 self.gsub(/([a-z])([A-Z0-9])/, '\1-\2').downcase
30 def find_all_positions x
43 self[0 .. 0].upcase + self[1 .. -1]
46 ## a very complicated regex found on teh internets to split on
47 ## commas, unless they occurr within double quotes.
49 split(/,\s*(?=(?:[^"]*"[^"]*")*(?![^"]*"))/)
56 cut = s[0 ... len].rindex(/\s/)
58 ret << s[0 ... cut] + "\n"
59 s = s[(cut + 1) .. -1]
61 ret << s[0 ... len] + "\n"
80 def in? range; range.member? self; end
84 def num_digits base=10
86 1 + (Math.log(self) / Math.log(10)).floor
90 if self < 128 && self >= 0
100 Hash[*self.map { |k, v| [k, v] unless o.include? k }.compact.flatten_one_level]
103 def select_by_value v=true
104 select { |k, vv| vv == v }.map { |x| x.first }
111 each_with_index { |x, i| ret << yield(x, i) }
115 def sum; inject(0) { |x, y| x + y }; end
119 each { |x| ret[x] = yield(x) }
123 # like find, except returns the value of the block rather than the
127 find { |e| ret ||= yield(e) }
132 best, bestval = nil, nil
135 if bestval.nil? || val < bestval
136 best, bestval = e, val
144 def flatten_one_level
145 inject([]) { |a, e| a + e }
148 def to_h; Hash[*flatten]; end
149 def rest; self[1..-1]; end
151 def to_boolean_h; Hash[*map { |x| [x, true] }.flatten]; end
153 ## apparently uniq doesn't use ==. wtf.
154 def remove_successive_dupes
169 sprintf "%012d", self
176 self + (60 - min) * 60
180 def midnight # within a second
181 self - (hour * 60 * 60) - (min * 60) - sec
184 def is_the_same_day? other
185 (midnight - other.midnight).abs < 1
188 def is_the_day_before? other
189 other.midnight - midnight <= 24 * 60 * 60 + 1
192 def to_nice_distance_s from=Time.now
193 later_than = (self < from)
194 diff = (self.to_i - from.to_i).abs.to_f
200 ["week", 4], # heh heh
203 ].argfind do |unit, size|
206 elsif size.nil? || diff < size
209 diff = (diff / size.to_f).round
220 TO_NICE_S_MAX_LEN = 9 # e.g. "Yest.10am"
221 def to_nice_s from=Time.now
224 elsif month != from.month
227 if is_the_same_day? from
229 elsif is_the_day_before? from
230 "Yest." + nearest_hour.strftime("%l%P")
238 ## simple singleton module. far less complete and insane than the ruby
239 ## standard library one, but automatically forwards methods calls and
240 ## allows for constructors that take arguments.
242 ## You must have #initialize call "self.class.i_am_the_instance self"
243 ## at some point or everything will fail horribly
246 def instance; @instance; end
247 def instantiated?; defined?(@instance) && !@instance.nil?; end
248 def method_missing meth, *a, &b
249 raise "no instance defined!" unless defined? @instance
250 @instance.send meth, *a, &b
252 def i_am_the_instance o
253 raise "there can be only one! (instance)" if defined? @instance
258 def self.included klass
259 klass.extend ClassMethods