Skip to content

Instantly share code, notes, and snippets.

@844196
Last active August 29, 2015 14:21
Show Gist options
  • Save 844196/2954d8b755868c2d7b24 to your computer and use it in GitHub Desktop.
Save 844196/2954d8b755868c2d7b24 to your computer and use it in GitHub Desktop.
rubyの練習ゥ...
#!/usr/bin/env ruby
#
# Print Renge Miyauchi quotes
#
# Author: Masaya Tk. (@844196)
# License: MIT
Version = 1.0
require 'optparse'
options = {}
OptionParser.new do |opt|
opt.on('-l', '--list', 'Print all quotes list and exit.') {|v| options[:l] = v }
opt.on('-f', '--file PATH', 'Specifiles the dictionary.') {|v| options[:f] = v }
opt.on('-n', '--number INT', 'Specify quote number.') {|v| options[:n] = v } # TODO: options[:n].to_iしたらいいんじゃね?
begin
opt.parse!(ARGV)
rescue
STDERR.puts "#{$0}: invalid option"
exit 1
end
end
class Character
attr_reader :quotes
# TODO: pathが存在しない、読み込み不可の場合の例外処理
def initialize(path)
@quotes = File.open(path).readlines
end
# TODO: 不正なquote_numberを渡された場合の例外処理
def say(quote_number)
puts @quotes[quote_number]
end
def list
@quotes.each_index {|i| puts "#{i} #{@quotes[i]}" }
end
end
renge = Character.new(options[:f] || '/usr/local/share/renge/renge-quotes')
if options[:l]
renge.list
exit
end
if options[:n].nil?
renge.say(rand(renge.quotes.length))
else
renge.say(options[:n].to_i)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment