Skip to content

Instantly share code, notes, and snippets.

@ksmxxxxxx
Last active August 16, 2020 09:04
Show Gist options
  • Save ksmxxxxxx/8e4378dec131910c1979436c3ec04eda to your computer and use it in GitHub Desktop.
Save ksmxxxxxx/8e4378dec131910c1979436c3ec04eda to your computer and use it in GitHub Desktop.
🍒本 4章の例題
def to_hex(r, g, b)
[r, g, b].inject('#') do |hex, n|
hex + n.to_s(16).rjust(2, '0')
end
end
def to_ints(hex)
r, g, b = hex[1..2], hex[3..4], hex[5..6]
[r, g, b].map(&:hex)
end
require 'minitest/autorun'
require './lib/convert_rgb'
class RgbTest < Minitest::Test
def test_to_hex
assert_equal '#000000', to_hex(0, 0, 0)
assert_equal '#ffffff', to_hex(255, 255, 255)
assert_equal '#043c78', to_hex(4, 60, 120)
end
def test_to_ints
assert_equal [0, 0, 0], to_ints('#000000')
assert_equal [255, 255, 255], to_ints('#ffffff')
# assert_equal '#043c78', to_hex(4, 60, 120)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment