]> git.cworth.org Git - sup/blobdiff - lib/sup/util.rb
better error handling
[sup] / lib / sup / util.rb
index ef2fdb1c7c7ea570939090b05793f2b1689c9841..dfe60acd8c490283d6f9ddf6e77f7962cf871f58 100644 (file)
@@ -43,6 +43,19 @@ class Object
   ## 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
@@ -176,19 +189,6 @@ class Array
   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
@@ -224,16 +224,16 @@ 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