The above function is giving me fits. First, it reads in some strings from a text file. Then, it reads in a text file that's a template for part of a HTML table. Next, it replaces certain keywords in the template file with the contents of the strings, and lastly, it pushes it all to a new text file (page_export.html).

The problem here is that some fields that are being imported in the text file are blank, or at least, I think that's the problem. Either way, I get this error:

To remedy this, not only did I declare a whitespace as a default argument for each string, but in another part of the script, I loop through each string - and if it's empty, I append a whitespace. Still no luck.

I have a function that's almost identical to the one above, but it operates over a slightly different set of data - one that doesn't have any empty strings - and it works great. I've also tested the code that appends the whitespace, and it works fine, too.

2 Answers
2

We can't possibly tell which argument is nil from the code provided, so check them all, and, assuming an error is thrown, start checking them individually. Add the following to the top of your function:

I guess I'm not quite understanding the concept of 'nil'... is nil its own datatype? As in, if something's nil, I can't append a whitespace to it, because a whitespace is of type string?
–
kivetrosMar 2 '11 at 20:55

nil is of type NilClass, and you cannot append strings to it. Whatever checking you think you're doing external to this function obviously isn't working. The only way that your error could be caused is if one of your arguments to gsub is nil.
–
meagar♦Mar 2 '11 at 21:01

they won't be nil, since in the method they get default values, isn't it?
–
m4risUMar 2 '11 at 21:03

@m4risU That's not how default values work. They can be nil, and almost certainly are nil. See my update.
–
meagar♦Mar 2 '11 at 21:05

Oh wow. Didn't know that, thanks a lot. Is there any way to replace the nil value with an empty string, though? If there's no data, I just need the field to be blank.
–
kivetrosMar 2 '11 at 21:06