From: Josh Cheek Date: 2011-09-14T23:23:35+09:00 Subject: Re: Tough Ruby Homework --f46d043bdfb825ec1304ace78036 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Sep 14, 2011 at 9:05 AM, Rory Pascua wrote: > I'm trying to take a long piece of text, find a word, and get that word > and the 3 words on either side of it and put that new "string" into > another variable. > > Example: > > I have a sentence like "Robert likes green beans, girls with moustaches, > and teddy bears. John thinks Robert is strange". I am searching for > the word "Robert", so I want to return the following: > > ["Robert likes green", "bears. John thinks Robert is strange."] > (doesn't have to be in an array, but you get the idea) > > I obviously use index to get the places where "Robert" can be found, but > any suggestion on how to do the rest? > > Bonus points: if you can do the same thing for multiple words...back to > the example, but search for "green AND teddy"...you'd get: > > ["Robert likes green beans, girls", "with moustaches, and teddy bears. > John thinks"] as a result. > > I'm posting this because I couldn't seem to find an easy way to do it.. > > -- > Posted via http://www.ruby-forum.com/. > > Check out String#split ( http://rdoc.info/stdlib/core/1.9.2/String#split-instance_method) that should help you get it into an array, which should be a lot easier to work with. --f46d043bdfb825ec1304ace78036--