Where is grep?

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 `<main>'

where shall 'grep' method come from?

thanks~

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 `<main>'

where shall 'grep' method come from?

thanks~

- --
Florian Gilcher

smtp: flo@andersground.net
jabber: Skade@jabber.ccc.de
gpg: 533148E2

Thanks, that sounds be more reasonable, b/c I like to grep from an
array not a string (yes perl does this).

btw, why 0/0 got failed but 0.0/0.0 seems valid?

irb(main):144:0> v=0/0
ZeroDivisionError: divided by 0
        from (irb):144:in `/'
        from (irb):144
        from /usr/bin/irb:12:in `<main>'

irb(main):145:0> v=0.0/0.0
=> NaN

···

2009/11/28 Florian Gilcher <flo@andersground.net>:

-----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"]

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

From: Ruby Newbee [mailto:rubynewbee@gmail.com]

Thanks, that sounds be more reasonable, b/c I like to grep from an
array not a string (yes perl does this).

btw, why 0/0 got failed but 0.0/0.0 seems valid?

irb(main):144:0> v=0/0
ZeroDivisionError: divided by 0
        from (irb):144:in `/'
        from (irb):144
        from /usr/bin/irb:12:in `<main>'

irb(main):145:0> v=0.0/0.0
=> NaN

It's the difference betweeen floating point and integer arithmetic.
When working with integers all results must be valid integers, whereas
floating point can represent NaN (Not a Number), negative zero,
and infinity and negative infinity:

irb(main):001:0> 1.0/0.0
=> Infinity
irb(main):002:0> -0.0
=> -0.0
irb(main):003:0> 0.0/-0.0
=> NaN
irb(main):004:0> -1.0/0.0
=> -Infinity
irb(main):005:0>

See Floating-point arithmetic - Wikipedia for more information.

Jep, there is always room to improve. It also maps better to the old behaviour where you were basically grepping #each_line.

Thanks,
Florian

···

On Nov 28, 2009, at 3:57 AM, David A. Black wrote:

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

Why not just string.scan(/pattern/) ?

Kind regards

  robert

···

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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Not the same thing

using 1.9

irb(main):001:0> a = "abe\nbob\nfred\njim"
=> "abe\nbob\nfred\njim"
irb(main):002:0> a.scan(/b/)
=> ["b", "b", "b"]
irb(main):003:0> a.lines.grep(/b/)
=> ["abe\n", "bob\n"]

a.lines.grep does the same thing that a.grep would do in 1.8, a.scan does not.

···

On Sat, Nov 28, 2009 at 11:10 AM, Robert Klemme <shortcutter@googlemail.com> wrote:

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/) ?

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Stupid me, of course! Thanks for the quick enlightenment. I somehow had assumed the text the OP was ultimately interested in was that matched by the expression. But that assumption is not covered by the posting.

Cheers

  robert

···

On 11/28/2009 05:35 PM, Rick DeNatale wrote:

On Sat, Nov 28, 2009 at 11:10 AM, Robert Klemme > <shortcutter@googlemail.com> wrote:

On 11/28/2009 08:27 AM, David A. Black wrote:

I would probably do this in 1.9:

  string.lines.grep(/pattern/)

where lines returns an enumerator.

Why not just string.scan(/pattern/) ?

Not the same thing

using 1.9

irb(main):001:0> a = "abe\nbob\nfred\njim"
=> "abe\nbob\nfred\njim"
irb(main):002:0> a.scan(/b/)
=> ["b", "b", "b"]
irb(main):003:0> a.lines.grep(/b/)
=> ["abe\n", "bob\n"]

a.lines.grep does the same thing that a.grep would do in 1.8, a.scan does not.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/