class Extract::MathWrapper
Attributes
str[RW]
Public Instance Methods
apply(l,r)
click to toggle source
# File lib/extract/math_calc.rb, line 18 def apply(l,r) #puts "apply call #{l} #{str} #{r}" raise "bad apply, not an operator" unless operator? #raise "bad apply, L #{l} R #{r}" unless l.to_s.present? && r.to_s.present? l.str = "0" if l.respond_to?(:str) && l.str.blank? r.str = "0" if r.respond_to?(:str) && r.str.blank? op = str op = "**" if op == "^" exp = "#{l.to_s} #{op} #{r.to_s}" return 0 if exp =~ /infinity/i || exp =~ /[a-z]/i #puts "evaling #{exp}" #puts "eval, L #{l.class} #{l.inspect} #{str} R #{r.inspect}" raise exp if exp =~ /[a-z]/i res = eval(exp) #puts "evaled #{exp} to #{res}" res end
left_associative?()
click to toggle source
# File lib/extract/math_calc.rb, line 8 def left_associative? true end
operator?()
click to toggle source
# File lib/extract/math_calc.rb, line 11 def operator? %w(+ - / * ^).include?(str) end
precedence()
click to toggle source
# File lib/extract/math_calc.rb, line 14 def precedence h = {"*" => 10, "/" => 10, "+" => 5, "-" => 5, "^" => 15} h[str] end
to_s()
click to toggle source
# File lib/extract/math_calc.rb, line 39 def to_s str end