From: botp Date: 2011-10-17T13:38:47+09:00 Subject: Re: writing a poem backwards or in reverse order 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