When calling another methods from class method , the method must be another class method. Why ? Because it doesn't make sense to call an instance method from a class method. What if there is no object instantiated.
def self.classify(number)
raise RuntimeError if number <= 0
sum = sum_of_factors(number)
if sum == number
"perfect"
elsif sum > number
"abundant"
elsif sum < number
"deficient"
end
end
def self.sum_of_factors(number)
factors = (1...number).select { |n| number % n == 0 }
sum_of_factors = factors.reduce(:+)
end
def self.classify(number)
raise RuntimeError if number <= 0
sum = sum_of_factors(number)
if sum == number
"perfect"
elsif sum > number
"abundant"
elsif sum < number
"deficient"
end
end
def self.sum_of_factors(number)
factors = (1...number).select { |n| number % n == 0 }
sum_of_factors = factors.reduce(:+)
end
Comments
Post a Comment