Class: Neovim::Calculator

Inherits:
Object show all
Defined in:
lib/neovim/tools/calculator.rb

Instance Method Summary collapse

Instance Method Details

#add(line) ⇒ Object



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
67
68
69
# File 'lib/neovim/tools/calculator.rb', line 40

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", "auto"                then auto!
      when /\A\d+\z/                  then @decs = $1.to_i
    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



38
# File 'lib/neovim/tools/calculator.rb', line 38

def auto!  ; @sep = nil ; end

#comma!Object



36
# File 'lib/neovim/tools/calculator.rb', line 36

def comma! ; @sep = "," ; end

#comma?Boolean

Returns:

  • (Boolean)


37
# File 'lib/neovim/tools/calculator.rb', line 37

def comma? ; @sep == "," ; end

#decs=(d) ⇒ Object



32
# File 'lib/neovim/tools/calculator.rb', line 32

def decs= d ; @decs = Integer d ; end

#dot!Object



34
# File 'lib/neovim/tools/calculator.rb', line 34

def dot!   ; @sep = "." ; end

#dot?Boolean

Returns:

  • (Boolean)


35
# File 'lib/neovim/tools/calculator.rb', line 35

def dot?   ; @sep == "." ; end

#reset!Object



28
29
30
# File 'lib/neovim/tools/calculator.rb', line 28

def reset!
  @result = nil
end

#resultObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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"
    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