# Suggestions for improving code

**URL:** <https://www.ruby-forum.com/t/suggestions-for-improving-code/79904>\
**Category:** Ruby\
**Created:** [March 11, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/suggestions-for-improving-code/79904 "2006-03-11T18:29:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adityam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@adityam](https://www.ruby-forum.com/u/adityam)\
**Post date:** [March 11, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/suggestions-for-improving-code/79904/1 "2006-03-11T18:29:21Z")

</div>

I am trying to convert a plain text file into a tex file with  
somemarkup. I have the following piece of code that does most of the  
work. It works fine but does not “look good”. Can someone suggest how  
can I improve this code.

---------------[snip]----------------------  
file = File.new(filename, ‘r’)  
texfile = File.open(basename + “.tex”, ‘w’)

CHAPTER = Regexp.new(“CHAPTER”)  
SPACES = Regexp.new("\s\s\s\s")  
BLANK = Regexp.new(/^\s\*$/)

chapter = true  
verse = false  
prev\_line = “”

file.each\_line do |line|  
if chapter && !BLANK.match(line)  
chapter = false  
texfile.puts “\chapter{#{line.chomp}}”  
elsif CHAPTER.match(line)  
chapter = true  
elsif verse && !SPACES.match(line)  
texfile.puts ‘\stoplines\stopnarrower’  
texfile.puts line.chomp  
verse = false  
elsif !verse && BLANK.match(prev\_line) && SPACES.match(line)  
texfile.puts ‘\startnarrower\startlines’  
texfile.puts line.chomp  
verse = true  
else  
texfile.puts line.chomp  
end  
prev\_line = line  
end  
------------------[snip]--------------------

Thanks,  
Aditya

---

<div class="post-metadata">

**Author:** ![adityam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@adityam](https://www.ruby-forum.com/u/adityam)\
**Post date:** [March 12, 2006, 8:24am UTC](https://www.ruby-forum.com/t/suggestions-for-improving-code/79904/2 "2006-03-12T08:24:35Z")

</div>

Aditya M. wrote:

> SPACES = Regexp.new(“\s\s\s\s”)  
> elsif CHAPTER.match(line)  
> texfile.puts line.chomp  
> [http://www.eecs.umich.edu/~adityam](http://www.eecs.umich.edu/~adityam) || Ph: 7342624008  
> ruby -p01e’gsub(/(\n\s\*\n)((?:^\s{4}._\n)+)/,  
> “\1\startnarrower\startlines\n\2\stoplines\stopnarrower\n”);  
> gsub(/(CHAPTER\s+)(._)\n/,“\chapter{\2}\n”)’ in \>out

---

<div class="post-metadata">

**Author:** ![adityam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@adityam](https://www.ruby-forum.com/u/adityam)\
**Post date:** [March 14, 2006, 8:02am UTC](https://www.ruby-forum.com/t/suggestions-for-improving-code/79904/3 "2006-03-14T08:02:00Z")

</div>

\<— On Mar 12, William J. wrote —\>

> > CHAPTER = Regexp.new(“CHAPTER”)  
> > texfile.puts “\chapter{#{line.chomp}}”  
> > else  
> > texfile.puts line.chomp  
> > end  
> > prev\_line = line  
> > end  
> > ------------------[snip]--------------------
> 
> ruby -p01e’gsub(/(\n\s\*\n)((?:^\s{4}._\n)+)/,  
> “\1\startnarrower\startlines\n\2\stoplines\stopnarrower\n”);  
> gsub(/(CHAPTER\s+)(._)\n/,"\chapter{\2}\n")’ in \>out

Thank you for the regex. The chapter part of your regex is better than  
what I was doing, it does not correctly identify narrower region.  
I think that I can tweak it a little to make it work correctly. But  
what I want to know is there a better way to do this in ruby.

I am not too comfortable with coding using a regex as it can be very  
difficult to maintain. Each time I have to look into the expression  
and try to understand it again.

Basically the logic of the program depends on the “state” which I am  
keeping track of using flags. Your code gets rid of the flags using a  
two pass algorithm. What are the pros and cons. In using a gsub, the  
program needs to read the entire file before it can make any changes.  
I thought that this would be memory inefficient but for a ~160 kb  
file, it is almost instantaneous. Even for a 3MB file it takes less  
than a second. At what file sizes should one read the file line by  
line rather than entire file in a single shot?

Thanks
