Skip to content

Instantly share code, notes, and snippets.

@bobishh
Last active November 15, 2016 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobishh/8f38f0dfdf151ce25602b946ca929ac8 to your computer and use it in GitHub Desktop.
Save bobishh/8f38f0dfdf151ce25602b946ca929ac8 to your computer and use it in GitHub Desktop.
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