Nesting a Ruby hash

Viewed 1622

I have a method that selects all the rows from my table like this:

smtp_status_raw = my_table.select(:message, :is_valid, :hostname).map { |h| h.values }

This returns an array that's like this:

[{:message=>"blah", :is_valid=>true, :hostname=>"1"}, {:message=>"blah", :is_valid=>true, :hostname=>"2"}, {:message=>"blah", :is_valid=>true, :hostname=>"3}]

Using the above information, I want to create a hash that looks like this:

{ 
:node_status => 
    {
        {:hostname => "1", :message: "blah"},
        {:hostname => "2", :message: "blah"},
        {:hostname => "3", :message: "blah"}
    }
}

First of all, my question - is it possible to create a hash like the above? In the above example Sequel query, I have three objects which are three separate hosts, and I want to add those three hosts into a :node_status key. Is that possible? If that's not a valid hash, what is an alternative?

Second, this is what I've tried:

# Initialize the hash
smtp_status_hash = { :node_status: => nil }

I've initialized the smtp_status_hash hash with a node_status key in it, but I am not sure how to nest the query results..

2 Answers
Related