Class: NumbersInWords::Fraction
- Inherits:
-
Object
- Object
- NumbersInWords::Fraction
- Defined in:
- lib/numbers_in_words/fraction.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#denominator ⇒ Object
readonly
Returns the value of attribute denominator.
-
#numerator ⇒ Object
readonly
Returns the value of attribute numerator.
Class Method Summary collapse
Instance Method Summary collapse
- #fraction ⇒ Object
- #in_words ⇒ Object
-
#initialize(denominator:, numerator: 1, attributes: nil) ⇒ Fraction
constructor
A new instance of Fraction.
- #lookup_keys ⇒ Object
- #ordinal ⇒ Object
Constructor Details
#initialize(denominator:, numerator: 1, attributes: nil) ⇒ Fraction
Returns a new instance of Fraction.
15 16 17 18 19 |
# File 'lib/numbers_in_words/fraction.rb', line 15 def initialize(denominator:, numerator: 1, attributes: nil) @denominator = denominator @numerator = numerator @attributes = attributes || NumbersInWords::ExceptionalNumbers::DEFINITIONS[denominator] || {} end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/numbers_in_words/fraction.rb', line 5 def attributes @attributes end |
#denominator ⇒ Object (readonly)
Returns the value of attribute denominator.
5 6 7 |
# File 'lib/numbers_in_words/fraction.rb', line 5 def denominator @denominator end |
#numerator ⇒ Object (readonly)
Returns the value of attribute numerator.
5 6 7 |
# File 'lib/numbers_in_words/fraction.rb', line 5 def numerator @numerator end |
Class Method Details
.in_words(that) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/numbers_in_words/fraction.rb', line 7 def self.in_words(that) r = that.rationalize(EPSILON) NumbersInWords .fraction(denominator: r.denominator, numerator: r.numerator) .in_words end |
Instance Method Details
#fraction ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/numbers_in_words/fraction.rb', line 40 def fraction if denominator == Float::INFINITY # We've reached the limits of ruby's number system # by the time we get to a googolplex (10 ** (10 ** 100)) return pluralize? ? 'infinitieths' : 'infinitieth' end pluralize? ? pluralized_fraction : singular_fraction end |
#in_words ⇒ Object
32 33 34 |
# File 'lib/numbers_in_words/fraction.rb', line 32 def in_words NumbersInWords.in_words(numerator) + ' ' + fraction end |
#lookup_keys ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/numbers_in_words/fraction.rb', line 21 def lookup_keys key = in_words key2 = strip_punctuation(key.split(' ')).join(' ') key3 = "a #{key}" key4 = "an #{key}" key5 = "a #{key2}" key6 = "an #{key2}" [key, key2, key3, key4, key5, key6].uniq end |
#ordinal ⇒ Object
36 37 38 |
# File 'lib/numbers_in_words/fraction.rb', line 36 def ordinal pluralize? ? pluralized_ordinal_in_words : singular_ordinal_in_words end |