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 } }
15 def defer_all_other_method_calls_to obj
16 class_eval %{ def method_missing meth, *a, &b; @#{obj}.send meth, *a, &b; end }
27 klass = klass.superclass
32 ## takes a value which it yields and then returns, so that code
35 ## x = expensive_operation
41 ## with(expensive_operation) { |x| log "got #{x}" }
43 ## i'm sure there's pithy comment i could make here about the
44 ## superiority of lisp, but fuck lisp.
45 def returning x; yield x; x; end
50 self.gsub(/([a-z])([A-Z0-9])/, '\1-\2').downcase
53 def find_all_positions x
66 self[0 .. 0].upcase + self[1 .. -1]
69 ## a very complicated regex found on teh internets to split on
70 ## commas, unless they occurr within double quotes.
72 split(/,\s*(?=(?:[^"]*"[^"]*")*(?![^"]*"))/)
79 cut = s[0 ... len].rindex(/\s/)
82 s = s[(cut + 1) .. -1]
91 def normalize_whitespace
92 gsub(/\t/, " ").gsub(/\r/, "")
107 def in? range; range.member? self; end
111 def num_digits base=10
112 return 1 if self == 0
113 1 + (Math.log(self) / Math.log(10)).floor
117 if self < 128 && self >= 0
127 Hash[*self.map { |k, v| [k, v] unless o.include? k }.compact.flatten_one_level]
130 def select_by_value v=true
131 select { |k, vv| vv == v }.map { |x| x.first }
138 each_with_index { |x, i| ret << yield(x, i) }
142 def sum; inject(0) { |x, y| x + y }; end
146 each { |x| ret[x] = yield(x) }
150 # like find, except returns the value of the block rather than the
154 find { |e| ret ||= yield(e) }
159 best, bestval = nil, nil
162 if bestval.nil? || val < bestval
163 best, bestval = e, val
171 def flatten_one_level
172 inject([]) { |a, e| a + e }
175 def to_h; Hash[*flatten]; end
176 def rest; self[1..-1]; end
178 def to_boolean_h; Hash[*map { |x| [x, true] }.flatten]; end
183 sprintf "%012d", self
190 self + (60 - min) * 60
194 def midnight # within a second
195 self - (hour * 60 * 60) - (min * 60) - sec
198 def is_the_same_day? other
199 (midnight - other.midnight).abs < 1
202 def is_the_day_before? other
203 other.midnight - midnight <= 24 * 60 * 60 + 1
206 def to_nice_distance_s from=Time.now
207 later_than = (self < from)
208 diff = (self.to_i - from.to_i).abs.to_f
214 ["week", 4.345], # heh heh
217 ].argfind do |unit, size|
220 elsif size.nil? || diff.round < size
221 "#{diff.round} #{unit}s"
234 TO_NICE_S_MAX_LEN = 9 # e.g. "Yest.10am"
235 def to_nice_s from=Time.now
238 elsif month != from.month
241 if is_the_same_day? from
243 elsif is_the_day_before? from
244 "Yest." + nearest_hour.strftime("%l%P")
252 ## simple singleton module. far less complete and insane than the ruby
253 ## standard library one, but automatically forwards methods calls and
254 ## allows for constructors that take arguments.
256 ## You must have #initialize call "self.class.i_am_the_instance self"
257 ## at some point or everything will fail horribly
260 def instance; @instance; end
261 def instantiated?; defined?(@instance) && !@instance.nil?; end
262 def method_missing meth, *a, &b
263 raise "no instance defined!" unless defined? @instance
264 @instance.send meth, *a, &b
266 def i_am_the_instance o
267 raise "there can be only one! (instance)" if defined? @instance
272 def self.included klass
273 klass.extend ClassMethods