Class: Numbers

Inherits:
Object show all
Defined in:
lib/xiki/numbers.rb

Class Method Summary collapse

Class Method Details

.enter_as_addedObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/xiki/numbers.rb', line 28

def self.enter_as_added
  txt = Clipboard[0]

  # If it has amount after " = ", make nure only they are added
  if txt =~ / = \$?\d/
    txt.gsub! /.* = /, ''
  end

  # If it has dollars, just add dollars
  if txt =~ /\$\d/
    View.insert self.sum_dollars(txt).to_s
    return
  end

  View.insert self.sum_numbers(txt).to_s

end


2
3
4
5
6
# File 'lib/xiki/numbers.rb', line 2

def self.menu
  ['.sum_numbers_in_clipboard',
    '.sum_dollars_in_clipboard'
  ]
end

.sum_dollars(txt) ⇒ Object



18
19
20
21
22
# File 'lib/xiki/numbers.rb', line 18

def self.sum_dollars txt
  numbers = txt.scan(/\$([\d.]+)/).map{|o| o.first}
  numbers.map!{|o| o.to_f}
  numbers.inject(0){|i, o| i + o}
end

.sum_dollars_in_clipboardObject



24
25
26
# File 'lib/xiki/numbers.rb', line 24

def self.sum_dollars_in_clipboard
  self.sum_dollars Clipboard[0]
end

.sum_numbers(txt) ⇒ Object



8
9
10
11
12
# File 'lib/xiki/numbers.rb', line 8

def self.sum_numbers txt
  numbers = txt.scan(/[\d.]+/)
  numbers.map!{|o| o.to_f}
  numbers.inject(0){|i, o| i + o}
end

.sum_numbers_in_clipboardObject



14
15
16
# File 'lib/xiki/numbers.rb', line 14

def self.sum_numbers_in_clipboard
  self.sum_numbers Clipboard[0]
end