Skip to content

Instantly share code, notes, and snippets.

@nathan-appere
Last active October 11, 2019 16: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 nathan-appere/40603bd71ba6756df94432b740c404e2 to your computer and use it in GitHub Desktop.
Save nathan-appere/40603bd71ba6756df94432b740c404e2 to your computer and use it in GitHub Desktop.
module Test
module RGB
include Kit::Contract
before ->(r:, g:, b:) { (0..255) === r && (0..255) === g && (0..255) === b }
after [
->(result:) { result.is_a?(String) },
->(result:) { result.start_with?('#') },
->(result:) { result.length.in?([4, 7]) },
]
def self.rgb_to_hex(r:, g:, b:)
"+" << [r, g, b].map { |el| el.to_s(16).rjust(2, '0') }.join
end
end
end
Test::RGB.rgb_to_hex(r: 128, g: 32, b: 12)
#RuntimeError:
# Kit::Contracts | after failure for `Test::RGB.rgb_to_hex`
# ["/Users/nathan/bootcamp/elearning/code/kit/libs/kit-organizer/spec/rgb_spec.rb", 11] ->(result:) { result.start_with?('#') },
# Called with: {:result=>"+80200c"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment