# Help with outfile.write(line) unless line =~ search\_text

**URL:** <https://www.ruby-forum.com/t/help-with-outfile-write-line-unless-line-search-text/202634>\
**Category:** Ruby\
**Created:** [February 2, 2011, 7:50pm UTC](https://www.ruby-forum.com/t/help-with-outfile-write-line-unless-line-search-text/202634 "2011-02-02T19:50:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 2, 2011, 7:50pm UTC](https://www.ruby-forum.com/t/help-with-outfile-write-line-unless-line-search-text/202634/1 "2011-02-02T19:50:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bobh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df705f/32.png) [@bobh](https://www.ruby-forum.com/u/bobh)\
**Post date:** [February 2, 2011, 8:09pm UTC](https://www.ruby-forum.com/t/help-with-outfile-write-line-unless-line-search-text/202634/2 "2011-02-02T20:09:17Z")

</div>

try if instead of unless

[http://en.wikibooks.org/wiki/Ruby\_Programming/Syntax/Control\_Structures](http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Control_Structures)

---

<div class="post-metadata">

**Author:** ![bobh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df705f/32.png) [@bobh](https://www.ruby-forum.com/u/bobh)\
**Post date:** [February 2, 2011, 8:49pm UTC](https://www.ruby-forum.com/t/help-with-outfile-write-line-unless-line-search-text/202634/3 "2011-02-02T20:49:31Z")

</div>

On 2/2/2011 1:08 PM, pat eyler wrote:

> On Wed, Feb 2, 2011 at 11:51 AM, Bob H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
> 
> [Ruby Programming/Syntax/Control Structures - Wikibooks, open books for an open world](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
