1

I'm learning Ruby and have failed to make a compound 'if' statement work. Here's my code (hopefully self explanatory)

commentline = Regexp.new('^;;') 
blankline = Regexp.new('^(\s*)$')

if (line !~ commentline || line !~ blankline)
  puts line
end

the variable 'line' is gotten from reading the following file:

;; alias filename backupDir

Prog_i  Prog_i.rb ./store
Prog_ii Prog_ii.rb ./store

This fails and I'm not sure why. Basically I want the comment lines and blank lines to be ignored during the processing of the lines in the file. Thanks for your help.

4
  • 3
    This really belongs on SO. A good rule to follow is if you are in front of your IDE, the question belongs on SO. If you are in front of your whiteboard it belongs on Programmers. Commented Dec 7, 2012 at 0:12
  • Walter, I believe you, but I'm at a loss to understand why it doesn't belong on "Programmers". I've read the docs, so could you help me out? Commented Dec 7, 2012 at 1:22
  • For reference, Ruby has a standard baked-in syntax for creating regexps. commentline = /^;;/ and blankline = /^(\s*)$/ respectively in your case. Commented Dec 7, 2012 at 13:35
  • @sawa - wow you studied logics in elementary school!!! I think I was studying manners then. Commented Dec 7, 2012 at 20:39

3 Answers 3

6

you need to use an AND

basically you want not (blank or comment) which turns into not blank and not comment after applying DeMorgan

if (line !~ commentline && line !~ blankline)
  puts line
end

or

unless(line ~= commentline || line ~= blankline)
  puts line
end

depending on which you find more readable

Sign up to request clarification or add additional context in comments.

4 Comments

There's also the unless approach.
To expand on what @tadman said, the syntax would become unless (line !~ commentLine || line !~ blankline. It's the same as negating the whole if statement (as @ratchet freak has done in his example here) - Edit: Oop, looks like Rachet's gone and updated his answer.
@KChaloux added that in the answer (I'm not really familiar with ruby syntax)
Thanks for the logic reminder, ratchet freak. You're correct.
1

You can write this much more terse, as

puts DATA.readlines.reject{|each|each =~ /^;;|^\s*$/}

__END__
;; alias filename backupDir

Prog_i  Prog_i.rb ./store
Prog_ii Prog_ii.rb ./store

2 Comments

Thanks akuhn. I put a modified version of this and it worked. The example I used was an MWE. When I stuck the various versions from the answers into my code a version of this was what I used. BTW, do you know why my question was migrated here from programmers? Thx.
@bev I guess it is because you ask very API specific questions, programmers is more about how to prepare for a job talk or on best practices for version control, etcetera.
1

This is your code:

commentline = Regexp.new('^;;') 
blankline = Regexp.new('^(\s*)$')

if (line !~ commentline || line !~ blankline)
  puts line
end

and how I'd write the same thing:

[
  ';; alias filename backupDir',
  '',
  'Prog_i  Prog_i.rb ./store',
  'Prog_ii Prog_ii.rb ./store'
].each do |line|

  puts line if (!line[/^(?:;;)?$/])

end

Which outputs:

;; alias filename backupDir
Prog_i  Prog_i.rb ./store
Prog_ii Prog_ii.rb ./store

5 Comments

+1. Didn't know you could use a Regexp within braces like that.
Notice what OP is doing. It is not the same as what you are doing. Yours is correct. The OP's condition always returns true because no string can match commentline and blankline at the same time.
Yes, or use a fixed-string search, or even capture a single element in a regex.
@sawa: Oh. Oops. Maybe I should "fix" mine to not work too? :-)
Even more Ruby, you could use a multiline string or a DATA segment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.