Skip to content

Instantly share code, notes, and snippets.

@cldwalker
Forked from jeroenvandijk/chars.rb
Created December 18, 2009 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cldwalker/259392 to your computer and use it in GitHub Desktop.
Save cldwalker/259392 to your computer and use it in GitHub Desktop.
Example of using Boson as an option parser
#!/usr/bin/env ruby
module Chars
extend self
def scale(args, options={})
scale = options[:scale]
chars = args.map { |x| Chars.char(x.to_i).map { |row| row.split("") } }
print_chars scale_vertically(scale_horizontally(chars, scale), scale)
end
private
def chars
@chars ||= [
[
" |",
" |",
" |",
" |",
" |"
],
[
" - ",
" |",
" - ",
"| ",
" _ "
],
[
" - ",
" |",
" - ",
" |",
" _ "
]
]
end
def char(i)
chars[i - 1]
end
def scale_horizontally(chars, scale)
chars.map do |row|
row.map do |cell|
[0, [1] * scale, 2].flatten.map { |i| cell[i] }
end
end
end
def scale_vertically(chars, scale)
chars.map do |row|
[0, [1] * scale, 2, [3] * scale, 4].flatten.map { |i| row[i] }
end
end
def print_chars(chars)
puts chars.map { |row| row.map{ |col| col.join } }.join("\n")
end
end
unless @loaded
@loaded = true
require 'rubygems'
require 'boson'
# Default scale is 10
parser = Boson::OptionParser.new(:scale=>10)
options = parser.parse(ARGV)
Chars.scale(parser.non_opts, options)
end
# Examples
#
# ./chars.rb 1 2 3
# ./chars.rb 1 2 -s=5
# ./chars.rb 1 2 3 --scale=2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment