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 47 48 |
# File 'lib/percentable/percent.rb', line 39 def * other if other.is_a? Percent self.class.new(to_f * other.value) elsif other.respond_to? :coerce a, b = other.coerce(self) a * b else raise TypeError, "#{other.class} can't be coerced into Percent" 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
74 75 76 |
# File 'lib/percentable/percent.rb', line 74 def coerce other [CoercedPercent.new(self), other] 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
61 62 63 |
# File 'lib/percentable/percent.rb', line 61 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 |