Class: Percentable::Percent
- Defined in:
- lib/percentable/percent.rb
Class Method Summary collapse
Instance Method Summary collapse
- #*(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #coerce(other) ⇒ Object
- #eql?(other) ⇒ Boolean
-
#initialize(value) ⇒ Percent
constructor
A new instance of Percent.
- #to_f ⇒ Object
- #to_i ⇒ Object
- #to_percent ⇒ Object
- #to_s ⇒ Object
- #value ⇒ Object
Constructor Details
Class Method Details
Instance Method Details
#*(other) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/percentable/percent.rb', line 39 def * other case other when Percent self.class.new(to_f * other.value) when Numeric self.class.new(value * other) end end |
#<=>(other) ⇒ Object
35 36 37 |
# File 'lib/percentable/percent.rb', line 35 def <=> other to_f <=> other.to_f end |
#==(other) ⇒ Object
27 28 29 |
# File 'lib/percentable/percent.rb', line 27 def == other (other.class == self.class && other.value == self.value) || other == self.to_f end |
#coerce(other) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/percentable/percent.rb', line 72 def coerce other case other when Numeric [AppliedPercent.new(self), other] else fail TypeError, "#{self.class} can't be coerced into #{other.class}" end end |
#eql?(other) ⇒ Boolean
31 32 33 |
# File 'lib/percentable/percent.rb', line 31 def eql? other self.send(:==, other) end |
#to_f ⇒ Object
19 20 21 |
# File 'lib/percentable/percent.rb', line 19 def to_f value/100 end |
#to_i ⇒ Object
23 24 25 |
# File 'lib/percentable/percent.rb', line 23 def to_i value.to_i end |
#to_percent ⇒ Object
59 60 61 |
# File 'lib/percentable/percent.rb', line 59 def to_percent self end |
#to_s ⇒ Object
15 16 17 |
# File 'lib/percentable/percent.rb', line 15 def to_s '%g%%' % value end |
#value ⇒ Object
11 12 13 |
# File 'lib/percentable/percent.rb', line 11 def value @value ||= 0.to_f end |