[SOLUTION] Ruby Quiz #14 LCD Numbers

  • Thread starter Nathaniel Talbott
  • Start date
N

Nathaniel Talbott

--Apple-Mail-9-295837381
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed

This was the first quiz I participated in (I've been waiting for one I
could whip out real quick), and it made a nice programming warm-up
after being away over the holidays. I added one small bit of
functionality: a colon, so I could do time displays. However, the colon
looked ugly when expanded, so I made it so it only expands vertically,
not horizontally.

The template-based approach I used could probably be used (with a bit
of tweaking) to allow prettier displays, but I didn't want to show off
how much I stink at ASCII art by trying myself :)

Thanks for the quiz!


Nathaniel
Terralien, Inc.

<:((><


--Apple-Mail-9-295837381
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
x-unix-mode=0744;
name="lcd"
Content-Disposition: attachment;
filename=lcd

#!/usr/bin/env ruby

module LCD
class Template
def self.height(size)
3 + (2 * size)
end

def initialize(string, option)
case option
when "h"
@horizontal_stable = true
end
@lines = string.gsub(/\./, ' ').split("\n")
end

def actual_line(line, size)
case
when line == 0
0
when line == (self.class.height(size) - 1)
4
when line < (size + 1)
1
when line == (size + 1)
2
else
3
end
end

def display(line, size)
l = @lines[actual_line(line, size)].dup
l[1, 1] *= size unless(@horizontal_stable)
l
end
end

TEMPLATES = {}
DATA.read.scan(/=([^\n])([a-z]?)\n([^=]+)/m){|c, option, t| TEMPLATES[c] = Template.new(t, option)}

def self.display(string, size=1)
output = ""
input = string.split(//)
Template.height(size).times do |i|
line = []
input.each do |character|
line << TEMPLATES[character].display(i, size)
end
output << "#{line.join(" " * size)}\n"
end
output
end
end

if(__FILE__ == $0)
require 'optparse'

size = 1
ARGV.options do |o|
o.on("-sSIZE", "--size", Integer){|size|}
end.parse!
puts LCD::display(ARGV.first, size)
end

__END__
=0
-.
|.|
...
|.|
-.
=1
...
..|
...
..|
...
=2
-.
..|
-.
|..
-.
=3
-.
..|
-.
..|
-.
=4
...
|.|
-.
..|
...
=5
-.
|..
-.
..|
-.
=6
-.
|..
-.
|.|
-.
=7
-.
..|
...
..|
...
=8
-.
|.|
-.
|.|
-.
=9
-.
|.|
-.
..|
-.
=:h
...
...
-.
...
-.
--Apple-Mail-9-295837381--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top