/ruby/raindrops/raindrops.rb

1
2
3
4
5
6
7
8
9
10
11
12
class Raindrops
  FRAGMENTS = { 3 => 'Pling', 5 => 'Plang', 7 => 'Plong' }

  def self.convert(n)
    s = FRAGMENTS.inject('') do |acc, (i, fragment)|
      acc << ((n % i == 0) ? fragment : '')
    end

    # Return the original digits if nothing matched
    s.empty? ?  n.to_s : s
  end
end

@eltabei thinks this looks great

Comments


You're not logged in right now. Please login via GitHub to comment