Last active
December 20, 2015 04:49
-
-
Save dcoxall/6073505 to your computer and use it in GitHub Desktop.
A quick script to demonstrate an easy way to execute ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'trollop' | |
class Command | |
def initialize(options = {}) | |
@default_name = options[:name] | |
end | |
def execute | |
name = ask "Hello, ...?" unless name = @default_name | |
say "Hello, #{name}!" | |
rescue Interrupt | |
puts "\n\n" | |
say "Okay then... Good bye!" | |
end | |
private | |
def say(text) | |
$stdout.puts text.to_s | |
end | |
def ask(text) | |
say(text) | |
$stdout.print '> ' | |
result = $stdin.gets.chomp | |
$stdout.puts | |
result | |
end | |
end | |
options = Trollop::options do | |
opt :name, "The default name to use", type: :string | |
end | |
command = Command.new(options) | |
command.execute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment