Skip to content

Instantly share code, notes, and snippets.

@The-Duchess
Last active December 6, 2015 11:45
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 The-Duchess/9f7a68c60b2ada95c566 to your computer and use it in GitHub Desktop.
Save The-Duchess/9f7a68c60b2ada95c566 to your computer and use it in GitHub Desktop.
given a text file ./QUOTES.txt formatted by line with {CHARACTER NAME} preceeding quotes by that character quotes.rb will display a random quote by that character
#-------~-----~--~------~---~-------~----~------------~---~------------~-----~--~------~---~-------~----~------------~---~-----
# quotes.rb
# Author: Alice "Duchess" Archer
# usage: [ruby] ./quotes.rb [<character name>]
# example: ruby ./quotes.rb Leonard Church
#-------~-----~--~------~---~-------~----~------------~---~------------~-----~--~------~---~-------~----~------------~---~-----
def file
@file = File.readlines("./QUOTES.txt")
end
def get_quotes character
lines = file
lines.each { |a| a.chomp! }
quote_lines = []
at_character = false
lines.each do |line|
if line =~ /\{.+\}/
if line =~ /\{#{character}\}/ and !at_character
at_character = true
next
elsif line =~ /\{.+\}/ and at_character
break;
end
end
if at_character
quote_lines.push(line)
end
end
return quote_lines
end
def pick_quote quote_lines
length = quote_lines.length
i = rand(length)
return quote_lines[i]
end
character = ""
0.upto(ARGV.length - 1) { |i| character.concat("#{ARGV[i]} ") }
character = character[0..-2].to_s
puts "#{pick_quote(get_quotes(character))} - #{character}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment