rornoob.com >

Beware of comments in ruby 1.8.7


9-Nov-2009

I can across a strange bug when upgrading to ruby enterprise 1.8.7 - one of my views displayed incorrectly after the upgrade but it was fine beforehand.

Here's an example of the code I'm referring to:

<table border="1">
<% @services.each do |service| %>
  <% next if service.status_id < 0 # skip inactive services %>
  <tr>
    <td><%= service.name %></td>
    <td><%= service.summary %></td>
  </tr>
<% end %>
</table>

Obviously I'm expecting a table that shows the name and summary of all active services.
i.e. something like this:

Email Notification User will receive email notification of events
Monthly Billing User account has been enabled for monthly billing
Free Reports User incurs no charge for viewing reports

That's exactly what I got under ruby 1.8.6, but look at the results when using ruby 1.8.7:

Email NotificationMonthly BillingFree Reports
User will receive email notification of events
User account has been enabled for monthly billing
User incurs no charge for viewing reports

Now why's that happening? The code is pretty basic so surely there's nothing that could go wrong.

Looking into the html source code I discovered that my code was generating this output:

<table border="1">


  Email Notification</td>
    <td>User will receive email notification of events</td>
  </tr>


  Monthly Billing</td>
    <td>User account has been enabled for monthly billing</td>
  </tr>


  Free Reports</td>
    <td>User incurs no charge for viewing reports</td>
  </tr>

</table>

i.e. note the missing <tr><td>

Strange...

Well it turns out that this line was the problem:

  <% next if service.status_id < 0 # skip inactive services %>

It's not the next or the if that causes things to go pear shaped - it's my comment! My comment has the effect of disabling the closing ruby tag so that nothing else is displayed until another closing tag is encountered, i.e. the one after displying the service name: <%= service.name %></td>

Strange that the same code worked fine using ruby 1.8.6, but at least the workaround is easy - just remove comments from the end of code lines, like so:

<table border="1">
<% @services.each do |service| %>
  <% next if service.status_id < 0 %>
  <tr>
    <td><%= service.name %></td>
    <td><%= service.summary %></td>
  </tr>
<% end %>
</table>

| | |

del.icio.usDiggblogmarksNewsvineStumbleUponTechnoratiFurlReddit

Add your comment
Name
Website
Heading
Comment