Class: NumbersInWords::ParseFractions

Inherits:
Object
  • Object
show all
Defined in:
lib/numbers_in_words/parsing/parse_fractions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nums) ⇒ ParseFractions

Returns a new instance of ParseFractions.



7
8
9
# File 'lib/numbers_in_words/parsing/parse_fractions.rb', line 7

def initialize(nums)
  @nums = nums.map(&:to_f)
end

Instance Attribute Details

#numsObject (readonly)

Returns the value of attribute nums.



5
6
7
# File 'lib/numbers_in_words/parsing/parse_fractions.rb', line 5

def nums
  @nums
end

Instance Method Details

#calculateObject



17
18
19
# File 'lib/numbers_in_words/parsing/parse_fractions.rb', line 17

def calculate
  (parse(numbers) * parse(fractions)).rationalize(EPSILON).to_f
end

#callObject



11
12
13
14
15
# File 'lib/numbers_in_words/parsing/parse_fractions.rb', line 11

def call
  return if no_fractions?

  just_fraction || calculate
end

#fractionsObject



29
30
31
# File 'lib/numbers_in_words/parsing/parse_fractions.rb', line 29

def fractions
  nums[index_of_fraction..-1]
end

#index_of_fractionObject



37
38
39
# File 'lib/numbers_in_words/parsing/parse_fractions.rb', line 37

def index_of_fraction
  nums.index { |n| n < 1.0 }
end

#just_fractionObject



33
34
35
# File 'lib/numbers_in_words/parsing/parse_fractions.rb', line 33

def just_fraction
  return nums.first if index_of_fraction.zero?
end

#no_fractions?Boolean

Returns:



41
42
43
# File 'lib/numbers_in_words/parsing/parse_fractions.rb', line 41

def no_fractions?
  nums.all? { |n| n.zero? || n >= 1.0 }
end

#numbersObject



25
26
27
# File 'lib/numbers_in_words/parsing/parse_fractions.rb', line 25

def numbers
  nums[0..index_of_fraction - 1]
end

#parse(numbers) ⇒ Object



21
22
23
# File 'lib/numbers_in_words/parsing/parse_fractions.rb', line 21

def parse(numbers)
  NumberParser.new.parse(numbers)
end