Help with outfile.write(line) unless line =~ search_text

Discussion in 'Ruby' started by Bob Hatch, Feb 2, 2011.

  1. Bob Hatch

    Bob Hatch Guest

    Rather than having the output write the line unless it equals
    search_text, I need it to write when it equals search_text. I know that
    should be easy, but I can't find the syntax.

    outfile.write(line) unless line =~ search_text

    --
    Posted via http://www.ruby-forum.com/.
     
    Bob Hatch, Feb 2, 2011
    #1
    1. Advertising

  2. Bob Hatch

    pat eyler Guest

    try if instead of unless

    http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Control_Structures

    On Wed, Feb 2, 2011 at 11:51 AM, Bob Hatch <> wrote:
    > Rather than having the output write the line unless it equals
    > search_text, I need it to write when it equals search_text. I know that
    > should be easy, but I can't find the syntax.
    >
    > outfile.write(line) unless line =3D~ search_text
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >




    --=20
    thanks,
    -pate
    -------------------------
    =A0Don't judge those who choose to sin differently than you do
     
    pat eyler, Feb 2, 2011
    #2
    1. Advertising

  3. Bob Hatch

    Jeremy Bopp Guest

    On 2/2/2011 1:08 PM, pat eyler wrote:
    > On Wed, Feb 2, 2011 at 11:51 AM, Bob Hatch <> wrote:
    >> Rather than having the output write the line unless it equals
    >> search_text, I need it to write when it equals search_text. I know that
    >> should be easy, but I can't find the syntax.
    >>
    >> outfile.write(line) unless line =~ search_text

    >
    > try if instead of unless
    >
    > http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Control_Structures


    Using if instead of unless is definitely clearer here, but you can also
    negate the match operator by replacing =~ with !~ as follows:

    outfile.write(line) unless line !~ search_text

    Double negatives are usually unnecessarily hard to understand at a
    glance though, so use the if statement instead of unless here.

    -Jeremy
     
    Jeremy Bopp, Feb 2, 2011
    #3
    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. solartimba

    simple outfile question

    solartimba, Sep 21, 2004, in forum: C++
    Replies:
    4
    Views:
    392
    David Hilsee
    Sep 22, 2004
  2. Replies:
    3
    Views:
    343
    Eric McCoy
    Dec 22, 2005
  3. Replies:
    0
    Views:
    258
  4. OUTFILE - like creating a log?

    , Dec 11, 2006, in forum: C Programming
    Replies:
    7
    Views:
    388
    goose
    Dec 13, 2006
  5. Gábor SEBESTYÉN

    Unless unless

    Gábor SEBESTYÉN, Jun 17, 2005, in forum: Ruby
    Replies:
    3
    Views:
    155
    Gábor SEBESTYÉN
    Jun 17, 2005
Loading...

Share This Page