]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/math.rb
plugin(oxford): fix empty service result see #37
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / math.rb
index f54733b33c0435e3e6a0d2ebd32a5e70f422b407..a6baa7090bb014b737776abbbd85d49906fb41b0 100644 (file)
@@ -39,12 +39,17 @@ class MathPlugin < Plugin
       expr.gsub!(/\b#{k}\b/, v)
     }
 
+    # ruby doesn't like floating-point values without a 0
+    # in front of them, so find any non-digit followed by
+    # a .<digits> and insert a 0 before the .
+    expr.gsub!(/(\D|^)(\.\d+)/,'\10\2')
+
     while expr =~ /(exp ([\w\d]+))/
       exp = $1
       val = Math.exp($2).to_s
       expr.gsub!(/#{Regexp.escape exp}/, "+#{val}")
     end
-    
+
     while expr =~ /^\s*(dec2hex\s*(\d+))\s*\?*/
       exp = $1
       val = sprintf("%x", $2)
@@ -56,13 +61,13 @@ class MathPlugin < Plugin
     while expr =~ /(log\s*((\d+\.?\d*)|\d*\.?\d+))\s*/
       exp = $1
       res = $2
-      
+
       if res == 0
         val = "Infinity"
       else
         val = Math.log(res).to_s
       end
-      
+
       expr.gsub!(/#{Regexp.escape exp}/, "+#{val}")
     end
 
@@ -110,9 +115,9 @@ class MathPlugin < Plugin
              answer = "a number with >30 digits..."
            end
          end
-         m.reply answer
+         m.reply answer.to_s
        rescue Exception => e
-         error "couldn't evaluate expression \"#{m.params}\": #{e.inspect}"
+         error e
          m.reply "illegal expression \"#{m.params}\""
          return
        end