Last active
December 17, 2015 23:49
-
-
Save kevbuchanan/5691996 to your computer and use it in GitHub Desktop.
From Best of Ruby Quiz by James Edward Gray II.
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
# This program dispalys LCD-style numbers in terminal. | |
# Digits to be displayed are passed as an argument from the command line. | |
# Size can be controled with the command-line option -s followed by a positive integer. | |
# example: $ ruby lcd_numbers.rb -s 5 1234 | |
class LCDNumber | |
def initialize (digits, size = 2, h_char = '-', v_char = '|' ) | |
@digits = digits.split(//) | |
@size = size | |
@h_char = h_char | |
@v_char = v_char | |
end | |
def horizontals(array) | |
@digits.each do |digit| | |
if array[digit.to_i] == 1 | |
print ' ' + @h_char * @size + ' ' | |
else | |
print ' ' + ' ' * @size + ' ' | |
end | |
end | |
puts | |
end | |
def verticals(array) | |
@size.times do | |
@digits.each do |digit| | |
if array[digit.to_i][0] == 1 | |
print ' ' + @v_char | |
else | |
print ' ' | |
end | |
print ' ' * @size | |
if array[digit.to_i][1] == 1 | |
print @v_char + ' ' | |
else | |
print ' ' | |
end | |
end | |
puts | |
end | |
end | |
def print_lcd | |
#characters for each level, top to bottom, for digits 0 to 9 | |
one = [1,0,1,1,0,1,1,1,1,1] | |
two = [[1,1],[0,1],[0,1],[0,1],[1,1],[1,0],[1,0],[0,1],[1,1],[1,1]] | |
three = [0,0,1,1,1,1,1,0,1,1] | |
four = [[1,1],[0,1],[1,0],[0,1],[0,1],[0,1],[1,1],[0,1],[1,1],[0,1]] | |
five = [1,0,1,1,0,1,1,0,1,0] | |
horizontals(one) | |
verticals(two) | |
horizontals(three) | |
verticals(four) | |
horizontals(five) | |
end | |
end | |
size = nil | |
if ARGV.size > 1 && ARGV[0] == '-s' | |
size = ARGV[1].to_i | |
ARGV.shift(2) | |
end | |
digits = ARGV[0] | |
if size != nil | |
myLCD = LCDNumber.new(digits, size) | |
else | |
myLCD = LCDNumber.new(digits) | |
end | |
myLCD.print_lcd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment