Skip to content

Instantly share code, notes, and snippets.

@grauwoelfchen
Last active December 14, 2015 04:29
Show Gist options
  • Save grauwoelfchen/5028710 to your computer and use it in GitHub Desktop.
Save grauwoelfchen/5028710 to your computer and use it in GitHub Desktop.
fibonacci
module Ex8
class Cli
def initialize(options={})
@options = options
end
def run
length = @options[:length].to_i
@output = []
length.times do |n|
@output << fibo(n)
end
p @output
end
def fibo(n)
return @output[n] if @output[n]
n < 2 ? n : fibo(n - 1) + fibo(n - 2)
end
=begin
def run
length = @options[:length].to_i
output = []
(0..length).inject([1,0]){ |m, i| output << m[1]; [m[1], m[0] + m[1]] }
p output
end
=end
end
end
#!/bin/env ruby
require 'pathname'
require 'optparse'
root = Pathname.new(__FILE__).realpath.parent.parent
$:.unshift root.join('lib') if $0 == __FILE__
require 'ex8'
def help
<<-BANNER
Useage:
...
BANNER
end
opts = {
:length => nil,
}
parser = OptionParser.new
parser.on('-l v', '--length v') { |v| opts[:length] = v }
parser.on('-v', '--version') { puts Ex8::VERSION; exit }
parser.on_tail('-h', '--help') { puts help; exit }
args = ARGV.dup
begin
parser.parse!(args)
rescue OptionParser::MissingArgument,
OptionParser::InvalidArgument,
OptionParser::InvalidOption
puts help
exit
end
begin
ui = Ex8::Cli.new(opts)
ui.run
rescue Interrupt
puts
end
require 'ex8/cli'
require 'ex8/version'
module Ex8
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment