Created
April 16, 2013 19:08
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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