Forum: Ruby regular expressions and conditionals

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Matt H. (Guest)
on 2008-10-26 00:24
(Received via mailing list)
I'm writting a little program to convert some source files to html.

I'm using regexp's to determine the content of each line and format it
accordingly. For example, if the line starts with a #, then it should be
output with a div (and id set to 'comment').

The problem is with matching the regular expressions. If I do this:

f = File.new("myfile", "r")
while !f.eof?
  line = f.gets

  if line.match(/^# (.*)/)
    puts "<div id='comment'>#{line.match(/^# (.*)/)[0]}</div>"
  end
end

Then not only do I get the commen lines formatted properly, I also get a
"nil" for every other line in the file.

How should I be using regexps in conditionals? I'm sure this should be
easy
but I've googled around for an hour and I can't find what's wrong.

TIA

Matt
Caleb C. (Guest)
on 2008-10-26 02:02
(Received via mailing list)
On 10/25/08, Matt H. <removed_email_address@domain.invalid> wrote:
>   line = f.gets
>
>   if line.match(/^# (.*)/)
>     puts "<div id='comment'>#{line.match(/^# (.*)/)[0]}</div>"
>   end
> end
>

I think what you're trying to write is a little more like this:
f = File.new("myfile", "r")
while line = f.gets
  if line.match(/^# (.*)/)
    line="<div id='comment'>#{line.chomp}</div>\n"
  end
  puts line
end

> Then not only do I get the commen lines formatted properly, I also get a
> "nil" for every other line in the file.

I didn't see any nils when I ran it... it just didn't print anything
but comments. Which would be because your original only printed lines
back out if they matched your comment pattern.

Why do you have a space after the # in your pattern? I've found it to
be good luck to always escape # when in regexps, even if it isn't
followed by a {. Probably a bug lurking in there.
Brian C. (Guest)
on 2008-10-26 11:44
while line = f.gets
  case line
  when /^# *(.*)/
    puts "<div id='comment'>#{$1}</div>"
  else
    puts line
  end
end
Robert K. (Guest)
on 2008-10-26 12:35
(Received via mailing list)
On 26.10.2008 01:02, Caleb C. wrote:
>> while !f.eof?
> while line = f.gets
>   if line.match(/^# (.*)/)
>     line="<div id='comment'>#{line.chomp}</div>\n"
>   end
>   puts line
> end

Even better is

# Use the block form of File.open!
File.new("myfile", "r") do |f|
   f.each do |line|
     line.chomp!
     case line
     when /^# *(.*)/
       puts "<div id='comment'># {$1}</div>"
     else
       puts line
     end
   end
end

or, for that matter

File.foreach("myfile") do |line|
   line.chomp!
   case line
   when /^# *(.*)/
     puts "<div id='comment'># {$1}</div>"
   else
     puts line
   end
end

Kind regards

  robert
This topic is locked and can not be replied to.