From: Rob Biedenharn Date: 2008-02-03T07:21:23+09:00 Subject: Re: Strings .each method On Feb 2, 2008, at 5:14 PM, Chris Hickman wrote: > Corey Haines wrote: >> but if you can describe why you want to do it, there might be a >> different way > > It is kind of hard to explain but I have a long string that someone > typed into a form which was then saved into a database. They used > made > up tags such as ", , , , ..." I > have to translate the string into html and add javascript etc. based > on > the different tags they wrote. > > I have it working for everything except one task. In this one task I > want to pass the words between two tags as a value for the javascript. > The problem is it is pulling the phrase apart at every space. I > know I > can add .each(???) and have it pull everything apart at the ??? but it > would involve a lot of adding ??? to this long string. > > What I am trying to do is add a while loop inside the block that will > look ahead at the next substring to see if it is a "". If > it > is I want it to exit the while loop and go on without throwing away > the > "" tag because it is needed next time through the block. > > Sorry if that didnt make sense at all. Perhaps something like this: irb> require 'enumerator' => true irb> a = "This is a big list of words" => "This is a big list of words" irb> (a.split(' ') << nil).each_cons(2) {|word,lookahead| puts "%-10s %s"%[word.inspect,lookahead.inspect]} "This" "is" "is" "a" "a" "big" "big" "list" "list" "of" "of" "words" "words" nil => nil Putting the << nil gives the last word something to be spit out of the each_cons with. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com