title: Ruby: Type Error: no implicit conversion of nil into String (TypeError)
body: <p>I am trying to parse an XML file, and looks like I am getting a type error, I don't really understand why its complaining about type, and I believe it breaks at the following record: </p>
  
  <pre><code>  &lt;row Id="30" PostTypeId="2" ParentId="25" CreationDate="2008-08-01T12:22:40.797" Score="7" Body="&amp;lt;p&amp;gt;I've had no trouble using the BSD sockets API in C++, in GNU/Linux. Here's the sample program I used:&amp;lt;/p&amp;gt;&amp;#xD;&amp;#xA;&amp;#xD;&amp;#xA;&amp;lt;pre&amp;gt;&amp;lt;code&amp;gt;#include &amp;amp;lt;sys/socket.h&amp;amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;int&amp;lt;br&amp;gt;main()&amp;lt;br&amp;gt;{&amp;lt;br&amp;gt;    return AF_INET;&amp;lt;br&amp;gt;}&amp;lt;br&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;&amp;#xD;&amp;#xA;&amp;#xD;&amp;#xA;&amp;lt;p&amp;gt;So my take on this is that z/OS is probably the complicating factor here, however, because I've never used z/OS before, much less programmed in it, I can't say this definitively. :-P&amp;lt;/p&amp;gt;" OwnerUserId="13" LastActivityDate="2008-08-01T12:22:40.797" CommentCount="1" /&gt;
  </code></pre>
  
  <p>Here is the type error:</p>
  
  <pre><code>load.rb:105:in `parse': no implicit conversion of nil into String (TypeError)
  </code></pre>
  
  <p>in my script this is the line that its complaining about </p>
  
  <pre><code>DateTime.parse(attributes['LastEditDate']).to_time.strftime("%F %T.%N"),
  DateTime.parse(attributes['LastActivityDate']).to_time.strftime("%F %T.%N"),
  attributes['Title'] == nil ? '' : attributes['Title'], 
  </code></pre>
  
  <p>Here is how my method looks like: </p>
  
  <pre><code>  def on_start_element(element, attributes)
      if element == 'row'
        @post_st.execute(attributes['Id'], attributes['PostTypeId'], attributes['AcceptedAnswerId'], attributes['ParentId'], attributes['Score'], attributes['ViewCount'], 
          attributes['Body'], attributes['OwnerUserId'] == nil ? -1 : attributes['OwnerUserId'], attributes['LastEditorUserId'], attributes['LastEditorDisplayName'], 
          DateTime.parse(attributes['LastEditDate']).to_time.strftime("%F %T.%N"), DateTime.parse(attributes['LastActivityDate']).to_time.strftime("%F %T.%N"), attributes['Title'] == nil ? '' : attributes['Title'], 
          attributes['AnswerCount'] == nil ? 0 : attributes['AnswerCount'], attributes['CommentCount'] == nil ? 0 : attributes['CommentCount'], 
          attributes['FavoriteCount'] == nil ? 0 : attributes['FavoriteCount'], DateTime.parse(attributes['CreationDate']).to_time.strftime("%F %T.%N"))
        post_id = attributes['Id']
  </code></pre>
  
  <p>I believe same code manage to parse previous record without a problem because when I looked at mysql dataabase that I am using to store the data had this record:</p>
  
  <pre><code>  &lt;row Id="27" PostTypeId="2" ParentId="11" CreationDate="2008-08-01T12:17:19.357" Score="13" Body="&amp;lt;p&amp;gt;@jeff&amp;lt;/p&amp;gt;&amp;#xD;&amp;#xA;&amp;#xD;&amp;#xA;&amp;lt;p&amp;gt;IMHO yours seems a little long.  However it does seem a little more robust with support for &amp;quot;yesterday&amp;quot; and &amp;quot;years&amp;quot;.  But in my experience when this is used the person is most likely to view the content in the first 30 days.  It is only the really hardcore people that come after that.  So that is why I usually elect to keep this short and simple.&amp;lt;/p&amp;gt;&amp;#xD;&amp;#xA;&amp;#xD;&amp;#xA;&amp;lt;p&amp;gt;This is the method I am currently using on one of my websites.  This only returns a relative day, hour, time.  And then the user has to slap on &amp;quot;ago&amp;quot; in the output.&amp;lt;/p&amp;gt;&amp;#xD;&amp;#xA;&amp;#xD;&amp;#xA;&amp;lt;pre&amp;gt;&amp;lt;code&amp;gt;public static string ToLongString(this TimeSpan time)&amp;lt;br&amp;gt;{&amp;lt;br&amp;gt;    string output = String.Empty;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;    if (time.Days &amp;amp;gt; 0)&amp;lt;br&amp;gt;        output += time.Days + &amp;quot; days &amp;quot;;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;    if ((time.Days == 0 || time.Days == 1) &amp;amp;amp;&amp;amp;amp; time.Hours &amp;amp;gt; 0)&amp;lt;br&amp;gt;        output += time.Hours + &amp;quot; hr &amp;quot;;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;    if (time.Days == 0 &amp;amp;amp;&amp;amp;amp; time.Minutes &amp;amp;gt; 0)&amp;lt;br&amp;gt;        output += time.Minutes + &amp;quot; min &amp;quot;;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;    if (output.Length == 0)&amp;lt;br&amp;gt;        output += time.Seconds + &amp;quot; sec&amp;quot;;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;    return output.Trim();&amp;lt;br&amp;gt;}&amp;lt;br&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;" OwnerUserId="17" LastEditorUserId="17" LastEditorDisplayName="Nick Berardi" LastEditDate="2008-08-01T13:16:49.127" LastActivityDate="2008-08-01T13:16:49.127" CommentCount="1" CommunityOwnedDate="2009-09-04T13:15:59.820" /&gt;
  </code></pre>