Ruby start_with? (), End_with? ()

I have a document with subject headings enclosed in "|" characters.

eg. "| LINKS |"

I want to check a line if it starts and ends with "|" to ensure that the specific title of the document object is valid. How to do it?

Source:

@filetarget = " < document file location here > "

@line = ""

file = File.new(@filetarget)

while (@line = file.gets)
   if((@line.start_with?("|")) and (@line.end_with?("|")))
      puts "Putting: " + @line
   end
end

The text of the document to parse:

| LINKS |

http://www.extremeprogramming.org/  <1>

http://c2.com/cgi/wiki?ExtremeProgramming  <2>

http://xprogramming.com/index.php  <3>

| COMMENTS |

* Test comment
* Test comment 2
* Test comment 3
+3
source share
2 answers

You can simply use a simple regular expression:

if line =~ /^\|.*\|$/

This way you can check other things, for example, the title should be complete and contain spaces around it ( /^\|\s[A-Z]+\s\|$/).

+5
source

Have you tried RDoc ?

"| LINKS |".start_with?("|") # => true
"| LINKS |".end_with?("|") # => true
+19
source

All Articles