Skip to content

Instantly share code, notes, and snippets.

@fables-tales
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save fables-tales/23524176b21e4fa79313 to your computer and use it in GitHub Desktop.

Select an option

Save fables-tales/23524176b21e4fa79313 to your computer and use it in GitHub Desktop.
class PlingPlangPlong
def initialize(number)
@number = number
end
def call
has_terms? ? joined_terms : number.to_s
end
private
attr_reader :number
def has_terms?
prime_factors.empty?
end
def joined_terms
prime_factors.reduce([]) { |accum, factor| accum << term_for(factor) }.join
end
def term_for(factor)
terms.fetch(factor)
end
def prime_factors
terms.keys.select {|x| number % x == 0}
end
def terms
{
3 => "Pling",
5 => "Plang",
7 => "Plong",
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment