Please Forward: Ruby Quiz Submission

People still work the old quizzes it seems...

James Edward Gray II

···

Begin forwarded message:

From: Greg Borenstein <greg.borenstein@gmail.com>
Date: June 8, 2006 3:43:17 PM CDT
To: submission@rubyquiz.com
Subject: Please Forward: Ruby Quiz Submission

Hello,

I am an intermediate Ruby on Rails programmer in Portland, Oregon. Recently, I was looking to improve my Ruby programming chops and I happened on the Ruby Quiz book. I've started working my way through the quizzes and am finding them very educational. I wanted to write to thank you for putting together this excellent project and to submit a solution.

I didn't know if it was appropriate to send the solution to an old quiz directly to the mailing list (where it might appear highly out of context) so I thought I'd send it to you first. Below, please find my solution to the LCD Numbers quiz. By using method missing, it was pretty easy for me to include hex characters (and, potentially, any others you could figure out how to draw) as you suggested in the additional exercises.

Anyway, thanks for the work you do on Ruby Quiz. I hope you enjoy my solution.

yours,

Greg
--
404
http://www.urbanhonking.com/ideasfordozens
http://www.atduskmusic.com

# QUIZ: LCD Numbers
# Ruby Quiz - LCD Numbers (#14)
# greg@mfdz.com
# 8 June 2006

require 'getoptlong'

S = " "
H = "-"
V = "|"

@size = 2

def m; "#{S}#{H * @size}#{S}"; end
def l; "#{V}#{S * ( @size + 1 )}"; end
def r;"#{S * (@size + 1) }#{V}"; end
def o;"#{V}#{S * @size}#{V}"; end
def n;"#{S * (@size + 2)}"; end

def method_missing( number, result )
  lines = case number
    when :one then %w(n r n r n)
    when :two then %w(m r m l m)
    when :three then %w(m r m r m)
    when :four then %w(n o m r n)
    when :five then %w(m l m r m)
    when :six then %w(m l m o m)
    when :seven then %w(m r n r n)
    when :eight then %w(m o m o m)
    when :nine then %w(m o m r n)
    when :zero then %w(m o n o m)
    when :a then %w(m o m o n)
    when :b then %w(n l m o m)
    when :c then %w(m l n l m)
    when :d then %w(n r m o m)
    when :e then %w(m l m l r)
    when :f then %w(m l m l n)
  end

  lines.each_index do |i|
    process_line i, send( lines[i].to_sym ), result
  end
  result
end

def process_line( number, string, result )
  case number
    when 0 then result[0] << string
    when 1 then @size.times{ |i| result[i+1] << string }
    when 2 then result[@size + 1] << string
    when 3 then @size.times{ |i| result[i+ @size + 2] << string }
    when 4 then result[result.length - 1] << string
  end
  result
end

def write_digit( n, arr )
  a = %w(zero one two three four five six seven eight nine)
  l = %w(a b c d e f)

  send "#{a[n.to_i]}".to_sym, arr if n == "0" or n.to_i != 0
  send "#{n}".to_sym, arr if l.include? n
end

#calculate the number of output lines based on the desired size
def lines( size )
  5 if size == 1
  ( 2 * size ) + 3
end

opts = GetoptLong.new( [ "-s", GetoptLong::OPTIONAL_ARGUMENT ] )
opts.each do |opt, arg|
  if opt == "-s" && (arg == "0" or arg.to_i != 0)
    @size = arg.to_i
  else
    puts "Usage: #$0 [-s SIZE] DIGITS"
    exit
  end
end
n = ARGV.shift

#initialize the array so that it has the right number of slots
arr = Array.new(lines(@size), "").collect!{|x| x * 2 } #to prevent weird recurring-object gotcha

numbers = n.scan(/\w/)

numbers.each do |n|
  write_digit( n, arr)
end

arr.each{|a| puts a }