rubynut
November 28, 2009, 3:57am
1
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~
rubynut
November 28, 2009, 4:16am
2
-----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 G.
On Nov 27, 2009, at 11:26 PM, Ruby N. wrote:
girl":String
from (irb):13
from /usr/bin/irb:12:in `’
where shall ‘grep’ method come from?
thanks~
Florian G.
smtp: [email protected]
jabber: [email protected]
gpg: 533148E2
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.12 (Darwin)
iEYEARECAAYFAksQlbgACgkQyLKU2FMxSOJ3eQCgkltVUHDzm4jPszZ1xh8Yn0Up
kGEAoKhK7atdiHhiKTT1l3F2fL0YK1bo
=qtur
-----END PGP SIGNATURE-----
rubynut
November 28, 2009, 4:49am
3
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
’
irb(main):145:0> v=0.0/0.0
=> NaN
2009/11/28 Florian G. [email protected] :
rubynut
November 28, 2009, 7:06am
4
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.
rubynut
November 28, 2009, 8:27am
5
Hi –
On Sat, 28 Nov 2009, Florian G. wrote:
x = “hello\nworld\nbaby\ngirl”
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
rubynut
November 28, 2009, 8:48am
6
On Nov 28, 2009, at 3:57 AM, David A. Black wrote:
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
rubynut
November 28, 2009, 5:10pm
7
On 11/28/2009 08:27 AM, David A. Black wrote:
where lines returns an enumerator.
Why not just string.scan(/pattern/) ?
Kind regards
robert
rubynut
November 28, 2009, 5:55pm
8
On 11/28/2009 05:35 PM, Rick DeNatale wrote:
On Sat, Nov 28, 2009 at 11:10 AM, Robert K.
[email protected] wrote:
On 11/28/2009 08:27 AM, David A. Black wrote:
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
rubynut
November 28, 2009, 5:36pm
9
On Sat, Nov 28, 2009 at 11:10 AM, Robert K.
[email protected] wrote:
Arrays are Enumerable and thus
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.
–
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