Skip to content

Instantly share code, notes, and snippets.

@sudheesh001
Created August 19, 2014 12:27
Show Gist options
  • Save sudheesh001/cb90d9b772759ede6e47 to your computer and use it in GitHub Desktop.
Save sudheesh001/cb90d9b772759ede6e47 to your computer and use it in GitHub Desktop.
# LCD.rb
# Execution : ruby LCD.rb -s 2 12345
### option parsing ###
s = $*.size > 1 ? $*.slice!(0..1)[1] : '2'
num = $*[0]
unless s =~ /^[1-9]\d*$/ and num =~ /^\d+$/
puts "Usage: #$0 [-s SIZE] DIGITS"
exit
end
s = s.to_i
### rendering ###
horizontal_hyphen = ' ' + '-'*s + ' ' # ' - '
vertical_dual_pipe = ['|' + ' '*s + '|']*s # '| |'
vertical_single_pipe_left = ['|' + ' '*s + ' ']*s # '| '
vertical_single_pipe_right = [' ' + ' '*s + '|']*s # ' |'
blank_key = ' ' * horizontal_hyphen.size # ' '
lcd = [
[horizontal_hyphen] << vertical_dual_pipe << blank_key << vertical_dual_pipe << horizontal_hyphen, # 0
[blank_key] << vertical_single_pipe_right << blank_key << vertical_single_pipe_right << blank_key, # 1
[horizontal_hyphen] << vertical_single_pipe_right << horizontal_hyphen << vertical_single_pipe_left << horizontal_hyphen, # 2
[horizontal_hyphen] << vertical_single_pipe_right << horizontal_hyphen << vertical_single_pipe_right << horizontal_hyphen, # 3
[blank_key] << vertical_dual_pipe << horizontal_hyphen << vertical_single_pipe_right << blank_key, # 4
[horizontal_hyphen] << vertical_single_pipe_left << horizontal_hyphen << vertical_single_pipe_right << horizontal_hyphen, # 5
[horizontal_hyphen] << vertical_single_pipe_left << horizontal_hyphen << vertical_dual_pipe << horizontal_hyphen, # 6
[horizontal_hyphen] << vertical_single_pipe_right << blank_key << vertical_single_pipe_right << blank_key, # 7
[horizontal_hyphen] << vertical_dual_pipe << horizontal_hyphen << vertical_dual_pipe << horizontal_hyphen, # 8
[horizontal_hyphen] << vertical_dual_pipe << horizontal_hyphen << vertical_single_pipe_right << horizontal_hyphen, # 9
]
### output ###
(0...(2*s+3)).each do |i|
puts num.chars.collect { |n| lcd[n.to_i].flatten[i] }.join ' '
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment