Sha256: 7df57f9b9f74c24ecf01f418102a490e573c942ee79547972153aec2a1a6949a

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

#! /usr/bin/env ruby
# coding: utf-8

require 'pp'


class Tefil::Calculator < Tefil::TextFilterBase
  def initialize(options = {})
    @options = options
    @preserve = options[:preserve]
    @ruby = options[:ruby]
    super(options)
  end

  def process_stream(in_io, out_io)
    in_io.each do |line|
      eq = line.chomp

      eq.gsub!(/\{/, '(' )
      eq.gsub!(/\}/, ')' )
      eq.gsub!(/\\times/, '*' )

      if @ruby
        eq.gsub!(/\^/, '**' )
        eq.gsub!(/sqrt/, 'Math::sqrt' )
        eq.gsub!(/log\(/, 'Math::log(' )
        eq.gsub!(/l\(/, 'Math::log(' )
        eq.gsub!(/exp\(/, 'Math::exp(' )
        eq.gsub!(/e\(/, 'Math::exp(' )
        result = eval(eq)
      else
        eq.gsub!(/\(/, '\(' )
        eq.gsub!(/\)/, '\)' )
        eq.gsub!(/\*/, '\*' )

        result = `echo  #{eq} | bc -l`.chomp
        result.sub!(/^\./, '0.')
        result.sub!(/^-\./, '-0.')
        result.sub!(/^0$/, '0.0')
      end

      out_io.print line.chomp + " = " if @preserve
      out_io.puts result
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tefil-0.1.5 lib/tefil/calculator.rb
tefil-0.1.4 lib/tefil/calculator.rb
tefil-0.1.3 lib/tefil/calculator.rb