# randomquotes3.rb # Random quotes - defining methods # Adapted from Kevin C. Baird, Ruby by Example, p. 16 # # ruby randomquotes3.rb quotes2.txt def pick_a_quote(filename) quotation_file = File.new(filename, 'r') # readlines returns an Array of Strings quotations = quotation_file.readlines() quotation_file.close() # remove newline chars str = quotations.join quotearray = str.split("\n") #each quote occupies 3 lines - quote, author, blankline random_index = rand(quotearray.size/3) quote = Array.new quote[0] = "\"" + quotearray[random_index*3] + "\"" quote[1] = quotearray[random_index*3+1] return quote end # Pass file name as first argument filename=ARGV[0] result = pick_a_quote(filename) puts result[0] puts result[1]