Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [SOLUTION] Ruby Quiz #14 LCD Numbers

Reply
Thread Tools

[SOLUTION] Ruby Quiz #14 LCD Numbers

 
 
Nathaniel Talbott
Guest
Posts: n/a
 
      01-09-2005
--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--


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[QUIZ] Gathering Ruby Quiz 2 Data (#189) Daniel Moore Ruby 10 01-31-2009 08:36 PM
[SOLUTION] Ruby Quiz #15 Animal Quiz David Tran Ruby 9 01-21-2005 02:11 AM
[QUIZ] LCD Numbers (#14) Ruby Quiz Ruby 26 01-14-2005 01:42 AM
[SOLUTION] Ruby Quiz #14 LCD Numbers email55555 email55555 Ruby 8 01-10-2005 11:14 PM
[SOLUTION] Ruby Quiz #14 LCD Numbers ( solution #2 ) email55555 email55555 Ruby 16 01-10-2005 04:56 PM



Advertisments