From: Robert Klemme Date: 2009-11-29T01:10:06+09:00 Subject: Re: where is grep? On 11/28/2009 08:27 AM, David A. Black wrote: > 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. Why not just string.scan(/pattern/) ? Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/