Three level hashes in ERB

315 views
Skip to first unread message

paul....@complex.com

unread,
Jan 16, 2014, 11:47:34 AM1/16/14
to puppet...@googlegroups.com
I have been trying to create a three level hash to dump into a template, but the deepest layer of the hash seems to not work.

My hash:

$clusters = {
    'Default' => {
      '127.0.0.1:11211' => { 
        'hostname' => '127.0.0.1', 
        'port' => '11211'
      },
    },
  }

My ERB template (modified for readability):
<% @clusters.sort.map do |k,v| -%>
  <% if v.is_a?(Hash) -%>
    '<%= k %>'
    <% @clusters[k].sort.map do |ki, vi| -%>
      <% if vi.is_a?(Hash) -%>
        '<%= ki %>'
        <% @clusters[ki].sort.map do |kii, vii| -%>
          <% if vi and vi != '' -%>
            '<%= kii %>' = '<%= vii %>',
          <% end -%>
        <% end -%>
      <% end -%>
    <% end -%>
  <% end -%>
<% end -%>

For some reason that I can't quite figure out, the innermost hash throws a Detail: undefined method `sort' for nil:NilClass error. I assume if ki was not a hash, the if vi.us_a?(Hash) would have been false and skipped that sort.

What am I doing wrong here?

Sebastiaan

unread,
Jan 16, 2014, 3:59:53 PM1/16/14
to puppet...@googlegroups.com
You are going through a hash inside a hash, so you have to call the
second sort on the hash inside the hash:

<% @clusters[k][ki].sort.map do |kii, vii| -%>

jcbollinger

unread,
Jan 17, 2014, 11:29:12 AM1/17/14
to puppet...@googlegroups.com


You're writing non-idiomatic Ruby, and you're embedding it into a template in a way that makes it hard to read.  I would write that template more like this:

<%
@clusters.select { |k, v| v.is_a?(Hash) }.sort.each_pair do |k,v|
-%>
  '<%= k %>'
<%
  v.select { |ki, vi| vi.is_a?(Hash) }.sort.each_pair do |ki, vi|
-%>
    '<%= ki %>'
<%
    vi.select { |kii, vii| vii and vii != '' }.sort.each_pair do |kii, vii|
-%>
      '<%= kii %>' = '<%= vii %>',
<%
    end
  end
end
-%>

Things to note:
  • You are iterating over key/value pairs, but you are not fully using the values provided to you.  You test them for whether they are Hashes, but then you go back and retrieve them again via their keys.  Relying strictly on the values passed into your blocks (as above) would avoid the indexing confusion you experienced and be clearer overall.
  • Embedding each Ruby line into your template separately makes everything harder to follow.  Moreover, doing it in the way you have done puts a bunch of extra whitespace into the template output.  (My version differed more from the original in this regard before I reduced the number of separate lines of Ruby code.)
  • The each_pair() method is better for your iteration purposes than is map() because each_pair() makes your intent clearer.  It's pointless and somewhat confusing to use map() and then throw away the result.
  • Using select() to filter your hashes prior to iteration, as above, is more idiomatic and clearer than testing the iterated values.  In this case it also cuts the number of levels of nesting in half.
  • In the innermost 'if' statement of your original code, you are testing variable 'vi', whereas it looks like you should be to testing 'vii' instead.


John

Reply all
Reply to author
Forward
0 new messages