Skip to content

Instantly share code, notes, and snippets.

@sevperez
Last active October 14, 2018 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sevperez/75cce560d684bb5488062ea982bfd82f to your computer and use it in GitHub Desktop.
Save sevperez/75cce560d684bb5488062ea982bfd82f to your computer and use it in GitHub Desktop.
class NumberAnalyzer
attr_accessor :numbers
def initialize(numbers)
@numbers = numbers
end
def print_factorials
numbers.each do |num|
puts "#{num}! = #{factorial(num)}"
end
end
private
def factorial(n)
if n == 0
1
else
n * factorial(n - 1)
end
end
end
analyzer = NumberAnalyzer.new([0, 1, 2, 3, 4, 5, 10])
analyzer.print_factorials
# Prints:
# 0! = 1
# 1! = 1
# 2! = 2
# 3! = 6
# 4! = 24
# 5! = 120
# 10! = 3628800
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment