where is grep?

R

Ruby Newbee

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~
 
F

Florian Gilcher

-----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 =20
the group of objects that mixed in Enumerable.

There, grep behaves like this (line by line):

x =3D "hello world baby girl"
x.grep(/hello/)
#=3D> ["hello world baby girl"]
x =3D "hello\nworld\nbaby\ngirl"
x.grep(/hello/)
#=3D> ["hello\n"]

The same can be done in Ruby 1.9 by just splitting the String =20
beforehand. Arrays are Enumerable and thus
still have grep:

x =3D "hello\nworld\nbaby\ngirl"
x.split("\n").grep(/hello/)
#=3D> ["hello"]

Note the absence of the line-break which might be intended or not :).

Regards,
Florian Gilcher

Hello,

I tried to grep from a string, but got wrong.

irb(main):010:0* x =3D "hello world baby girl"
=3D> "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 =20
girl":String
from (irb):13
from /usr/bin/irb:12:in `<main>'



where shall 'grep' method come from?

thanks~

- --
Florian Gilcher

smtp: (e-mail address removed)
jabber: (e-mail address removed)
gpg: 533148E2

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.12 (Darwin)

iEYEARECAAYFAksQlbgACgkQyLKU2FMxSOJ3eQCgkltVUHDzm4jPszZ1xh8Yn0Up
kGEAoKhK7atdiHhiKTT1l3F2fL0YK1bo
=3Dqtur
-----END PGP SIGNATURE-----
 
R

Ruby Newbee

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=3D0/0
ZeroDivisionError: divided by 0
from (irb):144:in `/'
from (irb):144
from /usr/bin/irb:12:in `<main>'

irb(main):145:0> v=3D0.0/0.0
=3D> NaN
 
W

Walton Hoops

From: Ruby Newbee [mailto:[email protected]]
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 http://en.wikipedia.org/wiki/Floating_point for more information.
 
D

David A. Black

Hi --

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
 
F

Florian Gilcher

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


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

Thanks,
Florian
 
R

Robert Klemme

Hi --

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
 
R

Rick DeNatale

Hi --

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 =3D "hello world baby girl"
x.grep(/hello/)
#=3D> ["hello world baby girl"]
x =3D "hello\nworld\nbaby\ngirl"
x.grep(/hello/)
#=3D> ["hello\n"]

The same can be done in Ruby 1.9 by just splitting the String beforehan= d.
Arrays are Enumerable and thus
still have grep:

x =3D "hello\nworld\nbaby\ngirl"
x.split("\n").grep(/hello/)
#=3D> ["hello"]

Note the absence of the line-break which might be intended or not :).

I would probably do this in 1.9:

=A0 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 =3D "abe\nbob\nfred\njim"
=3D> "abe\nbob\nfred\njim"
irb(main):002:0> a.scan(/b/)
=3D> ["b", "b", "b"]
irb(main):003:0> a.lines.grep(/b/)
=3D> ["abe\n", "bob\n"]

a.lines.grep does the same thing that a.grep would do in 1.8, a.scan does n=
ot.

--=20
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
 
R

Robert Klemme

On 11/28/2009 08:27 AM, David A. Black wrote:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top