This is an example of how to structure one-off Ruby scripts for easy growth, without making a big mess.
For more details on this, you can read my blog post about the idea or Confident Ruby by Avdi Grimm
This is an example of how to structure one-off Ruby scripts for easy growth, without making a big mess.
For more details on this, you can read my blog post about the idea or Confident Ruby by Avdi Grimm
#!/usr/bin/env ruby | |
require 'optparse' | |
at_exit do | |
options = parse_options | |
greet(options[:name]) | |
end | |
def parse_options(args = ARGV) | |
{ | |
name: 'world' | |
}.tap do |options| | |
parser = OptionParser.new do |opts| | |
opts.on('-n=NAME', '--name=NAME', 'Name to greet', String) do |value| | |
options[:name] = value | |
end | |
end | |
parser.parse!(args) | |
end | |
end | |
def greet(name) | |
puts "Hello, #{name}" | |
end |