From: Florian Gilcher Date: 2009-11-28T12:15:43+09:00 Subject: Re: where is grep? -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 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 :). Regards, Florian Gilcher On Nov 27, 2009, at 11:26 PM, Ruby Newbee wrote: > Hello, > > I tried to grep from a string, but got wrong. > > irb(main):010:0* x = "hello world baby girl" > => "hello world baby girl" > irb(main):011:0> > irb(main):012:0* > irb(main):013:0* x.grep(/hello/) > NoMethodError: undefined method `grep' for "hello world baby > girl":String > from (irb):13 > from /usr/bin/irb:12:in `
' > > > > where shall 'grep' method come from? > > thanks~ > - -- Florian Gilcher smtp: flo@andersground.net jabber: Skade@jabber.ccc.de gpg: 533148E2 -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.12 (Darwin) iEYEARECAAYFAksQlbgACgkQyLKU2FMxSOJ3eQCgkltVUHDzm4jPszZ1xh8Yn0Up kGEAoKhK7atdiHhiKTT1l3F2fL0YK1bo =qtur -----END PGP SIGNATURE-----