Refactoring a HTML class collector? I have following HTML class collector. Is there a better way than []tap for this?

def tr_classes(user)
  classes = [].tap do |c|
    c &lt;&lt; "error" if user.company.nil?
    c &lt;&lt; "disabled" if user.disabled?
  end
  if classes.any?
    " class=\"#{classes.join(" ")}\""
  end
end

&lt;tr&lt;%= tr_classes(user) %&gt;&gt;
  &lt;td&gt;&lt;%= user.name %&gt;&lt;/td&gt;
&lt;/tr&gt;
Notes:


This tap usage is not uncommon to see but IMHO it's very, very dubious. You can use a functional approach (see code below).
I wouldn't return the string along with the attribute, just the array of classes (which Rails3 helpers understand).


I'd write:

def tr_classes(user)
  [
    ("error" if !user.company),
    ("disabled" if user.disabled?),
  ].compact.presence
end


Tables are usually easier to build from helpers. You now would use the function that way:

content_tag(:tr, :class =&gt; tr_classes(user)) do
  content_tag(:td, user.name)
end