Skip to content

Instantly share code, notes, and snippets.

@daytoncleancoders
Created April 16, 2013 19:08
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 daytoncleancoders/5398669 to your computer and use it in GitHub Desktop.
Save daytoncleancoders/5398669 to your computer and use it in GitHub Desktop.
Tim and Rob's solution to the number to LCD kata in Ruby.
class InvalidIntegerError < RuntimeError
end
class NumberToLCD
@numbers = {
"1" => [" ",
" |",
" |"],
"2" => [" _ ",
" _|",
"|_ "],
"3" => [" _ ",
" _|",
" _|"],
"4" => [" ",
"|_|",
" |"],
"5" => [" _ ",
"|_ ",
" _|"],
"6" => [" _ ",
"|_ ",
"|_|"],
"7" => [" _ ",
" |",
" |"],
"8" => [" _ ",
"|_|",
"|_|"],
"9" => [" _ ",
"|_|",
" _|"],
"0" => [" _ ",
"| |",
"|_|"]
}
def self.convert number
unless number.is_a? Integer
raise InvalidIntegerError
end
result = ["","",""]
number.to_s.each_char do |i|
result[0] << @numbers[i][0]
result[1] << @numbers[i][1]
result[2] << @numbers[i][2]
end
result[0] << "\n"
result[1] << "\n"
result.join
end
end
require 'rspec'
require './numberToLCD'
describe 'NumberToLCD' do
it "should test" do
expect( 1 ).should == 1
end
it "should convert 1 to \n \n |\n |" do
expect NumberToLCD.convert(1).should ==
" \n" +
" |\n" +
" |"
end
it "should convert 2" do
expect NumberToLCD.convert(2).should ==
" _ \n" +
" _|\n" +
"|_ "
end
it "should convert 12" do
expect NumberToLCD.convert(12).should ==
" _ \n" +
" | _|\n" +
" ||_ "
end
it "should convert 1234567890" do
expect NumberToLCD.convert(1234567890).should ==
" _ _ _ _ _ _ _ _ \n" +
" | _| _||_||_ |_ ||_||_|| |\n" +
" ||_ _| | _||_| ||_| _||_|"
end
it "should convert 333" do
expect NumberToLCD.convert(333).should ==
" _ _ _ \n" +
" _| _| _|\n" +
" _| _| _|"
end
it "should throw an error when converting abc" do
lambda {NumberToLCD.convert("abc")}.should raise_error InvalidIntegerError
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment