bool_reader(*args)
bool_writer(*args)
end
+
+ def attr_reader_cloned *args
+ args.each { |sym| class_eval %{ def #{sym}; @#{sym}.clone; end } }
+ end
+
+ def defer_all_other_method_calls_to obj
+ class_eval %{ def method_missing meth, *a, &b; @#{obj}.send meth, *a, &b; end }
+ end
end
class Object
end
ret
end
+
+ ## takes a value which it yields and then returns, so that code
+ ## like:
+ ##
+ ## x = expensive_operation
+ ## log "got #{x}"
+ ## x
+ ##
+ ## now becomes:
+ ##
+ ## with(expensive_operation) { |x| log "got #{x}" }
+ ##
+ ## i'm sure there's pithy comment i could make here about the
+ ## superiority of lisp, but fuck lisp.
+ def returning x; yield x; x; end
+
+ ## clone of java-style whole-method synchronization
+ ## assumes a @mutex variable
+ def synchronized *meth
+ meth.each do
+ class_eval <<-EOF
+ alias unsynchronized_#{meth} #{meth}
+ def #{meth}(*a, &b)
+ @mutex.synchronize { unsynchronized_#{meth}(*a, &b) }
+ end
+ EOF
+ end
+ end
end
class String
self[0 .. 0].upcase + self[1 .. -1]
end
- ## found on teh internets
+ ## a very complicated regex found on teh internets to split on
+ ## commas, unless they occurr within double quotes.
def split_on_commas
split(/,\s*(?=(?:[^"]*"[^"]*")*(?![^"]*"))/)
end
while s.length > len
cut = s[0 ... len].rindex(/\s/)
if cut
- ret << s[0 ... cut] + "\n"
+ ret << s[0 ... cut]
s = s[(cut + 1) .. -1]
else
- ret << s[0 ... len] + "\n"
+ ret << s[0 ... len]
s = s[len .. -1]
end
end
ret << s
end
+
+ def normalize_whitespace
+ gsub(/\t/, " ").gsub(/\r/, "")
+ end
end
class Numeric
def rest; self[1..-1]; end
def to_boolean_h; Hash[*map { |x| [x, true] }.flatten]; end
-
- ## apparently uniq doesn't use ==. wtf.
- def remove_successive_dupes
- ret = []
- last = nil
- each do |e|
- unless e == last
- ret << e
- last = e
- end
- end
- ret
- end
end
class Time
["minute", 60],
["hour", 24],
["day", 7],
- ["week", 4], # heh heh
+ ["week", 4.345], # heh heh
["month", 12],
["year", nil],
].argfind do |unit, size|
- if diff <= 1
+ if diff.round <= 1
"one #{unit}"
- elsif size.nil? || diff < size
- "#{diff} #{unit}s"
+ elsif size.nil? || diff.round < size
+ "#{diff.round} #{unit}s"
else
- diff = (diff / size.to_f).round
+ diff /= size.to_f
false
end
end
end
end
- TO_NICE_S_MAX_LEN = 11 # e.g. "Jul 31 2005"
+ TO_NICE_S_MAX_LEN = 9 # e.g. "Yest.10am"
def to_nice_s from=Time.now
if year != from.year
- strftime "%b %e %Y"
+ strftime "%b %Y"
elsif month != from.month
strftime "%b %e"
else