If so, you calculate the average
as the total divided by the number of results, and return the average and the total. If not,
you return a zero total and nil as an average.
Next, let??™s take a look at the code that creates the PDF.
CHAPTER 6 n TRACKING AUCTIONS WITH EBAY 127
Dissecting the PDF Creation
The first thing that you do in the output routine in Listing 6-3 is define a new method
called escape_latex. This will take arbitrary strings and make them safe to be included
in a LaTeX script, so that characters that would usually have a special meaning will be
included as text literals instead. This method is added to the String class, so that it can
be called on any string. (This may seem very unusual to developers from nondynamic
language backgrounds, but it??™s quite a common Ruby idiom.)
class String
def latex_escape()
replacements= { '\\' =>'$\backslash$',
'$'=>'\$',
'%'=>'\%',
'&'=>'\&',
'_'=>'\_',
'~'=>'*~*',
'#'=>'\#',
'{'=>'$\{$',
'}'=>'$\}$'
}
self.gsub(/[#{replacements.keys.join('|')}]/) do |match|
replacements[match]
end
end
end
Note that many escape routines are simpler, but unfortunately, LaTeX has a fairly
complex set of escape sequences.
Pages:
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192