Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions exercises/practice/resistor-color-trio/.meta/example.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
class ResistorColorTrio
COLOR_CODES = %i[black brown red orange yellow green blue violet grey white].freeze
LABELS = %w[ohms kiloohms megaohms gigaohms teraohms].freeze

def initialize(colors)
@colors = colors.map(&:to_sym)
@tens, @ones, @zeros = @colors
end

def label
"Resistor value: #{to_s}"
"Resistor value: #{self}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So much better!

end

private

attr_reader :tens, :ones, :zeros

def significants
Expand All @@ -27,6 +27,12 @@ def value
end

def to_s
value < 1000 ? "#{value} ohms" : "#{(value.to_f/1000).to_i} kiloohms"
index = 0
value_copy = value
while value_copy >= 1000 && index < LABELS.size - 1
value_copy /= 1000
index += 1
end
"#{value_copy} #{LABELS[index]}"
end
end
55 changes: 50 additions & 5 deletions exercises/practice/resistor-color-trio/resistor_color_trio_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,71 @@
class ResistorColorTrioTest < Minitest::Test
def test_orange_and_orange_and_black
# skip
assert_equal "Resistor value: 33 ohms", ResistorColorTrio.new(%w[orange orange black]).label
actual = ResistorColorTrio.new(%w[orange orange black]).label
expected = 'Resistor value: 33 ohms'
assert_equal expected, actual
end

def test_blue_and_grey_and_brown
skip
assert_equal "Resistor value: 680 ohms", ResistorColorTrio.new(%w[blue grey brown]).label
actual = ResistorColorTrio.new(%w[blue grey brown]).label
expected = 'Resistor value: 680 ohms'
assert_equal expected, actual
end

def test_red_and_black_and_red
skip
assert_equal "Resistor value: 2 kiloohms", ResistorColorTrio.new(%w[red black red]).label
actual = ResistorColorTrio.new(%w[red black red]).label
expected = 'Resistor value: 2 kiloohms'
assert_equal expected, actual
end

def test_green_and_brown_and_orange
skip
assert_equal "Resistor value: 51 kiloohms", ResistorColorTrio.new(%w[green brown orange]).label
actual = ResistorColorTrio.new(%w[green brown orange]).label
expected = 'Resistor value: 51 kiloohms'
assert_equal expected, actual
end

def test_yellow_and_violet_and_yellow
skip
assert_equal "Resistor value: 470 kiloohms", ResistorColorTrio.new(%w[yellow violet yellow]).label
actual = ResistorColorTrio.new(%w[yellow violet yellow]).label
expected = 'Resistor value: 470 kiloohms'
assert_equal expected, actual
end

def test_blue_and_violet_and_blue
skip
actual = ResistorColorTrio.new(%w[blue violet blue]).label
expected = 'Resistor value: 67 megaohms'
assert_equal expected, actual
end

def test_minimum_possible_value
skip
actual = ResistorColorTrio.new(%w[black black black]).label
expected = 'Resistor value: 0 ohms'
assert_equal expected, actual
end

def test_maximum_possible_value
skip
actual = ResistorColorTrio.new(%w[white white white]).label
expected = 'Resistor value: 99 gigaohms'
assert_equal expected, actual
end

def test_first_two_colors_make_an_invalid_octal_number
skip
actual = ResistorColorTrio.new(%w[black grey black]).label
expected = 'Resistor value: 8 ohms'
assert_equal expected, actual
end

def test_ignore_extra_colors
skip
actual = ResistorColorTrio.new(%w[blue green yellow orange]).label
expected = 'Resistor value: 650 kiloohms'
assert_equal expected, actual
end
end