Last active
August 29, 2015 14:02
-
-
Save fables-tales/23524176b21e4fa79313 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 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