From: Josh Cheek Date: 2011-10-17T07:25:04+09:00 Subject: Re: writing a poem backwards or in reverse order --f46d044474291452f404af71f668 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Oct 16, 2011 at 5:14 PM, teresa nuagen wrote: > Josh Cheek wrote in post #1026925: > > On Sat, Oct 15, 2011 at 7:04 PM, Teresa Nguyen > > wrote: > > > >> Reads the poem into the program and writes it to the screen backwards > >> > >> I know that I have to apply using a string called "split" in order to > >> write backwards from last line to first line with the words in reverse > >> order. Can you help me? > >> > >> -- > >> Posted via http://www.ruby-forum.com/. > >> > >> > > > > Here are lots of little examples in cheatsheet format that might be > > helpful: > > > > > > https://github.com/JoshCheek/ruby-kickstart/blob/71603420eb55132b69f0018b9142e11b5ca561dc/cheatsheets/strings.rb > > > > > > https://github.com/JoshCheek/ruby-kickstart/blob/71603420eb55132b69f0018b9142e11b5ca561dc/cheatsheets/arrays.rb > > thanks for your response. I've been trying to write this program > rearranging a short poem. Can you give me an example of one method I can > use to do this: > > Reads the poem into the program and prints the poem to the screen > backwards from last line to first with the words of each line in reverse > order and the characters in each word in reverse order. > > -- > Posted via http://www.ruby-forum.com/. > > This is like your program 1, but just slightly different. You will need to write the reverse_words function yourself, try writing it separately from the rest of the program, and then when you are satisfied it is correct, use it as shown below. def reverse_words(line) end contents = File.read("poem") puts contents my_file = File.new("poem") line_array = my_file.readlines() line_array.reverse! line_array.each { |line| puts reverse_words(line) } my_file.close --f46d044474291452f404af71f668--