where is grep?

Discussion in 'Ruby' started by Ruby Newbee, Nov 28, 2009.

  1. Ruby Newbee

    Ruby Newbee Guest

    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~
    Ruby Newbee, Nov 28, 2009
    #1
    1. Advertising

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

    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 =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:
    jabber:
    gpg: 533148E2

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

    iEYEARECAAYFAksQlbgACgkQyLKU2FMxSOJ3eQCgkltVUHDzm4jPszZ1xh8Yn0Up
    kGEAoKhK7atdiHhiKTT1l3F2fL0YK1bo
    =3Dqtur
    -----END PGP SIGNATURE-----
    Florian Gilcher, Nov 28, 2009
    #2
    1. Advertising

  3. Ruby Newbee

    Ruby Newbee Guest

    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




    2009/11/28 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 the
    > group of objects that mixed in Enumerable.
    >
    > There, grep behaves like this (line by line):
    >
    > =C2=A0x =3D "hello world baby girl"
    > =C2=A0x.grep(/hello/)
    > =C2=A0#=3D> ["hello world baby girl"]
    > =C2=A0x =3D "hello\nworld\nbaby\ngirl"
    > =C2=A0x.grep(/hello/)
    > =C2=A0#=3D> ["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:
    >
    > =C2=A0x =3D "hello\nworld\nbaby\ngirl"
    > =C2=A0x.split("\n").grep(/hello/)
    > =C2=A0#=3D> ["hello"]
    >
    Ruby Newbee, Nov 28, 2009
    #3
  4. Ruby Newbee

    Walton Hoops Guest

    > From: Ruby Newbee [mailto:]
    >
    > 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.
    Walton Hoops, Nov 28, 2009
    #4
  5. 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
    David A. Black, Nov 28, 2009
    #5
  6. 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



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

    Thanks,
    Florian
    Florian Gilcher, Nov 28, 2009
    #6
  7. 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/
    Robert Klemme, Nov 28, 2009
    #7
  8. On Sat, Nov 28, 2009 at 11:10 AM, Robert Klemme
    <> 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 =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
    Rick DeNatale, Nov 28, 2009
    #8
  9. On 11/28/2009 05:35 PM, Rick DeNatale wrote:
    > On Sat, Nov 28, 2009 at 11:10 AM, Robert Klemme
    > <> 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.


    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

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Nov 28, 2009
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. danpres2k
    Replies:
    3
    Views:
    7,456
    danpres2k
    Aug 25, 2003
  2. John E. Jardine

    s/// has apparent side effect on grep()

    John E. Jardine, Apr 12, 2004, in forum: Perl
    Replies:
    2
    Views:
    435
    John Jardine
    Apr 13, 2004
  3. Al Belden

    perl vs Unix grep

    Al Belden, Jul 3, 2004, in forum: Perl
    Replies:
    1
    Views:
    5,169
    Giridhar Nandigam
    Jul 7, 2004
  4. Hans Bijvoet

    Grep

    Hans Bijvoet, Nov 19, 2004, in forum: Java
    Replies:
    5
    Views:
    812
    David Zimmerman
    Nov 20, 2004
  5. Spendius
    Replies:
    2
    Views:
    2,945
    Rogan Dawes
    Dec 13, 2004
Loading...

Share This Page