From: "David A. Black" Date: 2009-11-28T16:27:00+09:00 Subject: Re: where is grep? Hi -- On Sat, 28 Nov 2009, Florian Gilcher wrote: > Grep is not defined on Ruby 1.9-Strings. > > It was on Ruby 1.8-Strings, but only because they were belonging to the group > of objects that mixed in Enumerable. > > There, grep behaves like this (line by line): > > x = "hello world baby girl" > x.grep(/hello/) > #=> ["hello world baby girl"] > x = "hello\nworld\nbaby\ngirl" > x.grep(/hello/) > #=> ["hello\n"] > > The same can be done in Ruby 1.9 by just splitting the String beforehand. > Arrays are Enumerable and thus > still have grep: > > x = "hello\nworld\nbaby\ngirl" > x.split("\n").grep(/hello/) > #=> ["hello"] > > Note the absence of the line-break which might be intended or not :). I would probably do this in 1.9: string.lines.grep(/pattern/) where lines returns an enumerator. David -- THE COMPLEAT RUBYIST, Ruby training with Black/Brown/McAnally! January 22-23, Tampa, Florida Info and registration at http://www.thecompleatrubyist.com -------------------------------------- My new job: http://tinyurl.com/yfpn9hz