This is a very late posting...and I see that James already posted the
summary. I didn't have any time this past week/weekend to work on the
quiz when I thought I would. So here it is....
Thanks,
Zach
-------Code ---------
#!/usr/bin/ruby
class LcdChar
@@fragments = { 0 => ' ',
1 => ' - ',
2 => '| ',
3 => ' |',
4 => '| |', }
def initialize( arr )
@fragments = arr
end
def each_fragment
return unless block_given?
@fragments.each{ |e| yield @@fragments[e] }
end
end
class LcdDigit
class << self
@@digits = {1 => LcdChar.new( [ 0, 3, 0, 3, 0 ] ),
2 => LcdChar.new( [ 1, 3, 1, 2, 1 ] ),
3 => LcdChar.new( [ 1, 3, 1, 3, 1 ] ),
4 => LcdChar.new( [ 0, 4, 1, 3, 0 ] ),
5 => LcdChar.new( [ 1, 2, 1, 3, 1 ] ),
6 => LcdChar.new( [ 0, 2, 1, 4, 1 ] ),
7 => LcdChar.new( [ 1, 3, 0, 3, 0 ] ),
8 => LcdChar.new( [ 1, 4, 1, 4, 1 ] ),
9 => LcdChar.new( [ 1, 4, 1, 3, 0 ] ),
0 => LcdChar.new( [ 1, 4, 0, 4, 1 ] ) }
def get_digit( num )
num = num.to_i if( num.class == ''.class )
@@digits[ num ]
end
end
end
class LcdPrintWriter
class << self
def size_str( str, size )
nstr = str.dup
nstr[1,1] = nstr[1,1]*size
nstr
end
def print( digits, size=1 )
rows = []
digits.to_s.split( '' ).each do |n|
row = 0
horiz = true
LcdDigit.get_digit( n ).each_fragment do |e|
rows[row] ||= ''
rows[row] << LcdPrintWriter.size_str( e, size )
if not horiz
(1...size).each do
row += 1
rows[row] ||= ''
rows[row] << LcdPrintWriter.size_str( e, size )
end
end
horiz = !horiz
row += 1
end
end
puts rows
end
end
end
if( (sw=ARGV.shift) =~ /-s/ ) then
size = ARGV.shift
num = ARGV.shift
LcdPrintWriter.print( num , size.to_i )
else
puts "Unknown option #{sw}\nSyntax: \n\tlcd.rb -s size number_to_print"
end