Class: NumberMuncher::ToFraction

Inherits:
Object
  • Object
show all
Defined in:
lib/number_muncher/to_fraction.rb

Instance Method Summary collapse

Constructor Details

#initialize(unicode: true, separator: nil, factor: 0.001) ⇒ ToFraction

Returns a new instance of ToFraction.



3
4
5
6
7
# File 'lib/number_muncher/to_fraction.rb', line 3

def initialize(unicode: true, separator: nil, factor: 0.001)
  @unicode = unicode
  @separator = separator
  @factor = factor
end

Instance Method Details

#call(number, round_to: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/number_muncher/to_fraction.rb', line 9

def call(number, round_to: nil)
  return '' unless number

  @rational = rationalize(number, round_to)
  return '0' if rational.zero?

  separator = glyph ? '' : ' '
  parts = [whole, glyph || fraction].compact
  "#{sign}#{parts.map(&:to_s).join(separator)}"
end