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 ## found on teh internets
48 split(/,\s*(?=(?:[^"]*"[^"]*")*(?![^"]*"))/)
55 cut = s[0 ... len].rindex(/\s/)
57 ret << s[0 ... cut] + "\n"
58 s = s[(cut + 1) .. -1]
60 ret << s[0 ... len] + "\n"
79 def in? range; range.member? self; end
83 def num_digits base=10
85 1 + (Math.log(self) / Math.log(10)).floor
89 if self < 128 && self >= 0
99 Hash[*self.map { |k, v| [k, v] unless o.include? k }.compact.flatten_one_level]
102 def select_by_value v=true
103 select { |k, vv| vv == v }.map { |x| x.first }
110 each_with_index { |x, i| ret << yield(x, i) }
114 def sum; inject(0) { |x, y| x + y }; end
118 each { |x| ret[x] = yield(x) }
122 # like find, except returns the value of the block rather than the
126 find { |e| ret ||= yield(e) }
131 best, bestval = nil, nil
134 if bestval.nil? || val < bestval
135 best, bestval = e, val
143 def flatten_one_level
144 inject([]) { |a, e| a + e }
147 def to_h; Hash[*flatten]; end
148 def rest; self[1..-1]; end
150 def to_boolean_h; Hash[*map { |x| [x, true] }.flatten]; end
152 ## apparently uniq doesn't use ==. wtf.
153 def remove_successive_dupes
168 sprintf "%012d", self
175 self + (60 - min) * 60
179 def midnight # within a second
180 self - (hour * 60 * 60) - (min * 60) - sec
183 def is_the_same_day? other
184 (midnight - other.midnight).abs < 1
187 def is_the_day_before? other
188 other.midnight - midnight <= 24 * 60 * 60 + 1
191 def to_nice_distance_s from=Time.now
192 later_than = (self < from)
193 diff = (self.to_i - from.to_i).abs.to_f
199 ["week", 4], # heh heh
202 ].argfind do |unit, size|
205 elsif size.nil? || diff < size
208 diff = (diff / size.to_f).round
219 TO_NICE_S_MAX_LEN = 11 # e.g. "Jul 31 2005"
220 def to_nice_s from=Time.now
223 elsif month != from.month
226 if is_the_same_day? from
228 elsif is_the_day_before? from
229 "Yest." + nearest_hour.strftime("%l%P")
237 ## simple singleton module. far less complete and insane than the ruby
238 ## standard library one, but automatically forwards methods calls and
239 ## allows for constructors that take arguments.
241 ## You must have #initialize call "self.class.i_am_the_instance self"
242 ## at some point or everything will fail horribly
245 def instance; @instance; end
246 def instantiated?; defined?(@instance) && !@instance.nil?; end
247 def method_missing meth, *a, &b
248 raise "no instance defined!" unless defined? @instance
249 @instance.send meth, *a, &b
251 def i_am_the_instance o
252 raise "there can be only one! (instance)" if defined? @instance
257 def self.included klass
258 klass.extend ClassMethods