Skip to content
Snippets Groups Projects
Commit c1b08dff authored by Timothy Kimber's avatar Timothy Kimber
Browse files

Initial commit.

parents
No related branches found
No related tags found
No related merge requests found
# Ignore temp files
*~
# Ignore other junk files
*.backup
*.kate-swp
*.swp
# Contains data required by the script hello.rb
# Uses YAML format.
first:
last:
hello.rb 0 → 100755
#!/usr/bin/env ruby
require 'optparse'
require 'yaml'
require 'date'
class Parser
def self.parse_options(args)
options = {}
opt_parser = OptionParser.new do |opts|
opts.banner = "Greets whoever is identified by data.yml."
opts.separator "Will use the cowsay program, if it is installed."
opts.separator "Usage: hello.rb [options]"
opts.on("-c", "--cow COW", "Show message using cow type COW") do |c|
options["cow"] = c
end
opts.on("-h", "--help", "Prints this help") do
puts opts
exit
end
end
opt_parser.parse!(args)
return options
end
end
class Greeting
def self.greet
case DateTime.now.hour
when 0..11
"Good morning"
when 12..16
"Good afternoon"
else
"Good evening"
end
end
end
class MaybeCow
def self.command?(command)
system("which #{command} > /dev/null 2>&1")
end
def self.program(options)
primary = "/usr/games/cowsay"
alt = "/usr/bin/tee"
if command?(primary)
options["cow"] ? "#{primary} -f #{options["cow"]}" : primary
else
alt
end
end
end
options = Parser.parse_options(ARGV)
data = YAML.load_file('data.yml')
name =
if data["first"]
"#{data["first"]} #{data["last"]} (#{ENV["USER"]})"
else
"whoever you are"
end
IO.popen(MaybeCow.program(options), "w") do |c|
c.puts "#{Greeting.greet} #{name}!"
c.close
puts
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment