Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PingPongPrinter
- def initialize(range: 1..100, ping: 3, pong: 5)
- @range = range
- @ping = ping
- @pong = pong
- end
- def print
- @range.each {|n| p ping_pong_detect(n) }
- end
- private
- def ping_pong_detect(n)
- case n
- when ping_pong?
- 'APingBPong'
- when ping?
- 'APing'
- when pong?
- 'BPong'
- else
- n
- end
- end
- def ping?
- proc { |n| n % @ping == 0 }
- end
- def pong?
- proc { |n| n % @pong == 0 }
- end
- def ping_pong?
- proc { |n| n % (@ping * @pong) == 0 }
- end
- end
- PingPongPrinter.new.print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement