3 args.each { |sym| class_eval %{ def #{sym}?; @#{sym}; end } }
5 def bool_writer *args; attr_writer(*args); end
6 def bool_accessor *args
11 def attr_reader_cloned *args
12 args.each { |sym| class_eval %{ def #{sym}; @#{sym}.clone; end } }
23 klass = klass.superclass
28 ## takes a value which it yields and then returns, so that code
31 ## x = expensive_operation
37 ## with(expensive_operation) { |x| log "got #{x}" }
39 ## i'm sure there's pithy comment i could make here about the
40 ## superiority of lisp, but fuck lisp.
41 def with x; yield x; x; end
46 self.gsub(/([a-z])([A-Z0-9])/, '\1-\2').downcase
49 def find_all_positions x
62 self[0 .. 0].upcase + self[1 .. -1]
65 ## a very complicated regex found on teh internets to split on
66 ## commas, unless they occurr within double quotes.
68 split(/,\s*(?=(?:[^"]*"[^"]*")*(?![^"]*"))/)
75 cut = s[0 ... len].rindex(/\s/)
78 s = s[(cut + 1) .. -1]
87 def normalize_whitespace
88 gsub(/\t/, " ").gsub(/\r/, "")
103 def in? range; range.member? self; end
107 def num_digits base=10
108 return 1 if self == 0
109 1 + (Math.log(self) / Math.log(10)).floor
113 if self < 128 && self >= 0
123 Hash[*self.map { |k, v| [k, v] unless o.include? k }.compact.flatten_one_level]
126 def select_by_value v=true
127 select { |k, vv| vv == v }.map { |x| x.first }
134 each_with_index { |x, i| ret << yield(x, i) }
138 def sum; inject(0) { |x, y| x + y }; end
142 each { |x| ret[x] = yield(x) }
146 # like find, except returns the value of the block rather than the
150 find { |e| ret ||= yield(e) }
155 best, bestval = nil, nil
158 if bestval.nil? || val < bestval
159 best, bestval = e, val
167 def flatten_one_level
168 inject([]) { |a, e| a + e }
171 def to_h; Hash[*flatten]; end
172 def rest; self[1..-1]; end
174 def to_boolean_h; Hash[*map { |x| [x, true] }.flatten]; end
176 ## apparently uniq doesn't use ==. wtf.
177 def remove_successive_dupes
192 sprintf "%012d", self
199 self + (60 - min) * 60
203 def midnight # within a second
204 self - (hour * 60 * 60) - (min * 60) - sec
207 def is_the_same_day? other
208 (midnight - other.midnight).abs < 1
211 def is_the_day_before? other
212 other.midnight - midnight <= 24 * 60 * 60 + 1
215 def to_nice_distance_s from=Time.now
216 later_than = (self < from)
217 diff = (self.to_i - from.to_i).abs.to_f
223 ["week", 4], # heh heh
226 ].argfind do |unit, size|
229 elsif size.nil? || diff < size
232 diff = (diff / size.to_f).round
243 TO_NICE_S_MAX_LEN = 9 # e.g. "Yest.10am"
244 def to_nice_s from=Time.now
247 elsif month != from.month
250 if is_the_same_day? from
252 elsif is_the_day_before? from
253 "Yest." + nearest_hour.strftime("%l%P")
261 ## simple singleton module. far less complete and insane than the ruby
262 ## standard library one, but automatically forwards methods calls and
263 ## allows for constructors that take arguments.
265 ## You must have #initialize call "self.class.i_am_the_instance self"
266 ## at some point or everything will fail horribly
269 def instance; @instance; end
270 def instantiated?; defined?(@instance) && !@instance.nil?; end
271 def method_missing meth, *a, &b
272 raise "no instance defined!" unless defined? @instance
273 @instance.send meth, *a, &b
275 def i_am_the_instance o
276 raise "there can be only one! (instance)" if defined? @instance
281 def self.included klass
282 klass.extend ClassMethods