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

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

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

On Wed, Feb 2, 2011 at 11:51 AM, Bob H. [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs