Created
March 7, 2015 17:22
-
-
Save johnkpaul/6e183c0a027505d2fcaf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SumOfFactors | |
def initialize(until_num, *divisible_by) | |
@until_num = until_num | |
@divisible_by = divisible_by | |
end | |
def get_sum | |
get_range.select(&self.method(:is_disivible_by)) | |
.inject(&self.method(:sum_arr)) | |
end | |
private | |
def sum_arr(sum, num) | |
sum + num | |
end | |
def is_disivible_by num | |
@divisible_by.any? {|divisible_by| num % divisible_by == 0 } | |
end | |
def get_range | |
(1..@until_num) | |
end | |
end | |
sum = SumOfFactors.new(100, 3, 5).get_sum | |
puts sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment