class R0man::Number
Public Class Methods
new(str)
click to toggle source
# 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
parse(str)
click to toggle source
# File lib/r0man/number.rb, line 13 def parse(str) new(str) end
Public Instance Methods
value()
click to toggle source
# 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
Private Instance Methods
digits_are_valid()
click to toggle source
# File lib/r0man/number.rb, line 29 def digits_are_valid add_error("Number contains invalid characters: #{@invalid.map(&:name).join(", ")}") unless @invalid.empty? end
maximum_consecutive_counts_are_inside_limits()
click to toggle source
# File lib/r0man/number.rb, line 33 def maximum_consecutive_counts_are_inside_limits MaximumConsecutiveCount.for(@valid).each do |digit, count| tolerance = digit.max_consecutive_allowed add_error("#{digit} occurs #{count} times whereas it is allowed only #{tolerance} times") if count > tolerance end end
previous_digit_are_allowed()
click to toggle source
# File lib/r0man/number.rb, line 40 def previous_digit_are_allowed @valid.each_cons(2) do |d1, d2| add_error("#{d1} cannot be subtracted from #{d2}") if (!d1.allows_next_digit?(d2) && d2.greater_than?(d1)) end end
same_digit_is_not_added_to_and_subtracted_from_a_larger_digit()
click to toggle source
# File lib/r0man/number.rb, line 46 def same_digit_is_not_added_to_and_subtracted_from_a_larger_digit @valid.each_cons(3) do |d1, d2, d3| add_error("#{d1} cannot be added to and subtracted from #{d2}") if (d1 == d3 && d2.greater_than?(d1)) end end