Class: Percentage

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/chance/percentage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Percentage

Returns a new instance of Percentage.



6
7
8
# File 'lib/chance/percentage.rb', line 6

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/chance/percentage.rb', line 4

def value
  @value
end

Instance Method Details

#*(other) ⇒ Object



30
31
32
33
# File 'lib/chance/percentage.rb', line 30

def *(other)
  other_value = other.kind_of?(Numeric) ? other : other.value
  Percentage.new value * other_value
end

#<=>(other) ⇒ Object



26
27
28
# File 'lib/chance/percentage.rb', line 26

def <=>(other)
  value <=> other.value
end

#chanceObject



14
15
16
# File 'lib/chance/percentage.rb', line 14

def chance
  Chance.new self
end

#of(number_or_percentage) ⇒ Object



10
11
12
# File 'lib/chance/percentage.rb', line 10

def of(number_or_percentage)
  number_or_percentage * (value / 100.0)
end

#to_fObject



18
19
20
# File 'lib/chance/percentage.rb', line 18

def to_f
  @value.to_f
end

#to_sObject



22
23
24
# File 'lib/chance/percentage.rb', line 22

def to_s
  to_f.to_s
end