Class: Neovim::Calculator
Instance Method Summary collapse
- #add(line) ⇒ Object
- #auto! ⇒ Object
- #colon! ⇒ Object
- #colon? ⇒ Boolean
- #comma! ⇒ Object
- #comma? ⇒ Boolean
- #decs=(d) ⇒ Object
- #dot! ⇒ Object
- #dot? ⇒ Boolean
- #reset! ⇒ Object
- #result ⇒ Object
Instance Method Details
#add(line) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/neovim/tools/calculator.rb', line 47 def add line line = line.chomp line.slice! /#.*/ if $& =~ /!(\w+)/ then case $1 when "c", "comma", "k", "komma" then comma! when "l", "colon" then colon! when "d", "dot", "p", "point" then dot! when "a", "auto" then auto! when /\A\d+\z/ then @decs = $&.to_i when "all", "full", "places" then @decs = nil end end line.slice! /^.*:/ line.strip! line.notempty? or return products = [] line.split /(?:[;|&]|\s+[,+-]\s+)/ do |p| products.push [ (split_products p), $& =~ /-/] end minus = false products.each { |p,nm| if not @result then @result = p elsif minus then @result -= p else @result += p end minus = nm } @result end |
#auto! ⇒ Object
45 |
# File 'lib/neovim/tools/calculator.rb', line 45 def auto! ; @sep = nil ; end |
#colon! ⇒ Object
43 |
# File 'lib/neovim/tools/calculator.rb', line 43 def colon! ; @sep = ":" ; end |
#colon? ⇒ Boolean
44 |
# File 'lib/neovim/tools/calculator.rb', line 44 def colon? ; @sep == ":" ; end |
#comma! ⇒ Object
41 |
# File 'lib/neovim/tools/calculator.rb', line 41 def comma! ; @sep = "," ; end |
#comma? ⇒ Boolean
42 |
# File 'lib/neovim/tools/calculator.rb', line 42 def comma? ; @sep == "," ; end |
#decs=(d) ⇒ Object
37 |
# File 'lib/neovim/tools/calculator.rb', line 37 def decs= d ; @decs = Integer d ; end |
#dot! ⇒ Object
39 |
# File 'lib/neovim/tools/calculator.rb', line 39 def dot! ; @sep = "." ; end |
#dot? ⇒ Boolean
40 |
# File 'lib/neovim/tools/calculator.rb', line 40 def dot? ; @sep == "." ; end |
#reset! ⇒ Object
33 34 35 |
# File 'lib/neovim/tools/calculator.rb', line 33 def reset! @result = nil end |
#result ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/neovim/tools/calculator.rb', line 13 def result @result or return r = @decs ? (@result.round @decs) : @result case r when BigDecimal then r = r.to_s "F" if @decs.nonzero? then r.sub! /(\.)(\d+)\z/ do (@sep||$1) + ($2.to_s.ljust @decs, "0") end else r.slice! /\.0+\z/ end when Integer then r = r.to_s if @decs.nonzero? then r << (@sep||".") << "0"*@decs end end r end |