From: teresa nuagen Date: 2011-10-17T14:02:03+09:00 Subject: Re: writing a poem backwards or in reverse order botp wrote in post #1026966: > On Mon, Oct 17, 2011 at 12:25 PM, teresa nuagen > wrote: >> I'm struggling on how to approach the method using "string#split" I'm >> unsure of how to divide each line into words and iterating it from last >> word to first word, and then writing it in reverse order. > > break down the problem into smaller ones. > > eg, > > s=<<-HERE > waiting for hours > wet at first, in the cold dark > just to be gulped down > HERE > #=> "waiting for hours\nwet at first, in the cold dark\njust to be > gulped down\n" > > a1=s.split "\n" > #=> ["waiting for hours", "wet at first, in the cold dark", "just to > be gulped down"] > > a2=a1.reverse > #=> ["just to be gulped down", "wet at first, in the cold dark", > "waiting for hours"] > > a3=a2.map{|s| s.split.reverse.join(" ")} > #=> ["down gulped be to just", "dark cold the in first, at wet", > "hours for waiting"] > > a4=a3.map{|s| s.split.map{|w| w.split(//).reverse.join}} > #=> [["nwod", "deplug", "eb", "ot", "tsuj"], ["krad", "dloc", "eht", > "ni", ",tsrif", "ta", "tew"], ["sruoh", "rof", "gnitiaw"]] > > a4=a3.map{|s| s.split.map{|w| w.split(//).reverse.join}.join(" ")} > #=> ["nwod deplug eb ot tsuj", "krad dloc eht ni ,tsrif ta tew", > "sruoh rof gnitiaw"] > > puts a4 > nwod deplug eb ot tsuj > krad dloc eht ni ,tsrif ta tew > sruoh rof gnitiaw > #=> nil alright, so lets say this is an example: While moon sets atop the trees, leaves cling to rain. and I have to transform it into this: rain. to cling leaves trees, the atop sets moon While The program I will be writing is a lot longer poem, so I'm just using this short haiku as an example. Can you help me approach it using the same method as how you explained it above? -- Posted via http://www.ruby-forum.com/.