https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?15064139052014-03-04T14:07:55ZRuby Issue Tracking SystemRuby trunk - Bug #9582: CSV#headers not giving expected results.https://bugs.ruby-lang.org/issues/9582?journal_id=456242014-03-04T14:07:55Znagachika (Tomoyuki Chikanaga)nagachika00@gmail.com
<ul><li><strong>Tracker</strong> changed from <i>Backport</i> to <i>Bug</i></li><li><strong>Project</strong> changed from <i>Backport200</i> to <i>Ruby trunk</i></li><li><strong>ruby -v</strong> set to <i>-</i></li></ul> Ruby trunk - Bug #9582: CSV#headers not giving expected results.https://bugs.ruby-lang.org/issues/9582?journal_id=456292014-03-05T05:19:23Ztlewin (Thiago Lewin)thiago.lewin@gmail.com
<ul><li><strong>File</strong> <a href="/attachments/4269/9582.patch">9582.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/4269/9582.patch">9582.patch</a> added</li></ul><p>The CSV::Table#headers method just look at the first row of the table, if you enable the option &quot;:return_headers&quot; then should work.<br>
In order to keep the API consistency, I attached a patch that iterates through the table until find a row with header information.</p>
Ruby trunk - Bug #9582: CSV#headers not giving expected results.https://bugs.ruby-lang.org/issues/9582?journal_id=456572014-03-06T16:26:49ZJEG2 (James Gray)jeg2@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>Yeah, I also looked at making this change, but the behavior of returning just the first line is documented:</p>
<p><a href="https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L672">https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L672</a></p>
<p>I&#39;m not sure if users of this library count on that or not. This change would alter the behavior of tables, so at the very least I think it would need to happen on a major version release.</p>
<p>Tables are also editable, so deleting rows affects the results of what this method returns.</p>
<p>Anyway, if you need the non-first row headers, you can always do:</p>
<p>header_row = table.find { |row| !row.headers.empty? }<br>
headers = header_row &amp;&amp; header_row.headers</p>