Skip to content

Instantly share code, notes, and snippets.

@timuruski
Created December 2, 2016 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timuruski/4649d5caf1a273423f63e57cedacedea to your computer and use it in GitHub Desktop.
Save timuruski/4649d5caf1a273423f63e57cedacedea to your computer and use it in GitHub Desktop.
Greeter script structure example

Structure of Ruby Scripts

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment