Class: R0man::Number
- Inherits:
-
Object
- Object
- R0man::Number
- Includes:
- SimpleValidation
- Defined in:
- lib/r0man/number.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(str) ⇒ Number
constructor
A new instance of Number.
- #value ⇒ Object
Constructor Details
#initialize(str) ⇒ Number
Returns a new instance of Number.
18 19 20 21 |
# File 'lib/r0man/number.rb', line 18 def initialize(str) digits = str.each_char.map { |d| Digit.parse(d) } @valid, @invalid = digits.partition(&:valid?) end |
Class Method Details
.parse(str) ⇒ Object
13 14 15 |
# File 'lib/r0man/number.rb', line 13 def parse(str) new(str) end |
Instance Method Details
#value ⇒ Object
23 24 25 |
# File 'lib/r0man/number.rb', line 23 def value @value ||= @valid.each_cons(2).reduce(0) { |result, (d1, d2)| result += d1.value_compared_to(d2) } + @valid.last.value end |