Class: NumbersInWords::ParseFractions
- Inherits:
-
Object
- Object
- NumbersInWords::ParseFractions
- Defined in:
- lib/numbers_in_words/parsing/parse_fractions.rb
Instance Attribute Summary collapse
-
#nums ⇒ Object
readonly
Returns the value of attribute nums.
Instance Method Summary collapse
- #calculate ⇒ Object
- #call ⇒ Object
- #fractions ⇒ Object
- #index_of_fraction ⇒ Object
-
#initialize(nums) ⇒ ParseFractions
constructor
A new instance of ParseFractions.
- #just_fraction ⇒ Object
- #no_fractions? ⇒ Boolean
- #numbers ⇒ Object
- #parse(numbers) ⇒ Object
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
#nums ⇒ Object (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
#calculate ⇒ Object
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 |
#call ⇒ Object
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 |
#fractions ⇒ Object
29 30 31 |
# File 'lib/numbers_in_words/parsing/parse_fractions.rb', line 29 def fractions nums[index_of_fraction..-1] end |
#index_of_fraction ⇒ Object
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_fraction ⇒ Object
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
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 |
#numbers ⇒ Object
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 |