Class: Neovim::Calculator
Instance Method Summary collapse
- #add(line) ⇒ Object
- #comma! ⇒ Object
- #comma? ⇒ Boolean
- #decs=(d) ⇒ Object
- #dot! ⇒ Object
- #dot? ⇒ Boolean
- #reset! ⇒ Object
- #result ⇒ Object
Instance Method Details
#add(line) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/neovim/tools/calculator.rb', line 38 def add line line = line.chomp line.slice! /#.*/ if $& =~ /!(\w+)/ then case $1 when "c", "comma", "k", "komma" then comma! when "d", "dot", "p", "point" then dot! when /\A\d+\z/ then @decs = $1.to_i end end line.slice! /^.*:/ line.strip! line.notempty? or return products = [] line.split /(?<![,.])[+-]/ 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 |
#comma! ⇒ Object
34 |
# File 'lib/neovim/tools/calculator.rb', line 34 def comma! ; @sep, @grp = ",", "." ; end |
#comma? ⇒ Boolean
36 |
# File 'lib/neovim/tools/calculator.rb', line 36 def comma? ; @sep == "," ; end |
#decs=(d) ⇒ Object
31 |
# File 'lib/neovim/tools/calculator.rb', line 31 def decs= d ; @decs = Integer d ; end |
#dot! ⇒ Object
33 |
# File 'lib/neovim/tools/calculator.rb', line 33 def dot! ; @sep, @grp = ".", "," ; end |
#dot? ⇒ Boolean
35 |
# File 'lib/neovim/tools/calculator.rb', line 35 def dot? ; @sep == "." ; end |
#reset! ⇒ Object
27 28 29 |
# File 'lib/neovim/tools/calculator.rb', line 27 def reset! @result = nil end |
#result ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/neovim/tools/calculator.rb', line 13 def result r = @result.round @decs case r when BigDecimal then r = r.to_s "F" r.sub! /(?:(\.)([0-9]+))?\z/ do (@sep||$1) + ($2.to_s.ljust @decs, "0") end when Integer then r = r.to_s end r end |