Last active
November 15, 2016 12:43
-
-
Save bobishh/8f38f0dfdf151ce25602b946ca929ac8 to your computer and use it in GitHub Desktop.
This file contains 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 JoinFormatter | |
def initialize(separator = '', default = '') | |
@default = default | |
end | |
def format(elements) | |
res_string = elements.join '' | |
return @default if res_string == '' | |
res_string | |
end | |
end | |
class CheckRunner | |
def initialize(checks, formatter, element) | |
@checks = checks | |
@formatter = formatter | |
@element = element | |
end | |
def result | |
@result ||= run | |
end | |
private | |
def run | |
@formatter.format(check_results) | |
end | |
def check_results | |
@checks.map { |c| c.call(@element) } | |
end | |
end | |
def fizz_buzz(number) | |
fizz = ->(x){ 'fizz' if x % 3 == 0 } | |
buzz = ->(x){ 'buzz' if x % 5 == 0 } | |
CheckRunner.new([fizz, buzz], JoinFormatter.new('', number), number).result | |
end | |
def fizz_buzz_list(list) | |
list_checker = ->(x) { x.map { |e| fizz_buzz(e) } } | |
CheckRunner.new([list_checker], JoinFormatter.new('\n', 'no elements'), list).result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment