vivek_jonam vivek_jonam - 4 months ago 8
Ruby Question

Is it a valid Ruby Mash syntax?

I find the below mash declared in the readme of https://github.com/hw-cookbooks/haproxy :

haproxy 'myhaproxy' do
config Mash.new(
:global => {
:maxconn => node[:haproxy][:global_max_connections],
:user => node[:haproxy][:user],
:group => node[:haproxy][:group]
},
:defaults => {
:log => :global,
:mode => :tcp,
:retries => 3,
:timeout => 5
},
:frontend => {
:srvs => {
:maxconn => node[:haproxy][:frontend_max_connections],
:bind => "#{node[:haproxy][:incoming_address]}:#{node[:haproxy][:incoming_port]}",
:default_backend => :backend_servers
}
},
:backend => {
:backend_servers => {
:mode => :tcp,
:server => [
"an_node 192.168.99.9:9999" => {
:weight => 1,
:maxconn => node[:haproxy][:member_max_connections]
}
]
}
}
)
end


I wanted to know what the below Mash denote:

:server => [
"an_node 192.168.99.9:9999" => {
:weight => 1,
:maxconn => node[:haproxy][:member_max_connections]
}
]


Is it an array of hash?

Answer

This is valid ruby syntax and yes, this will produce an array with one element, which is a hash.

[
  "an_node 192.168.99.9:9999" => {
    :weight => 1,
    :maxconn => node[:haproxy][:member_max_connections]
  }
] 
# => [{"an_node 192.168.99.9:9999"=>{:weight=>1, :maxconn=>2}}]

Mashes are completely irrelevant here, by the way. Mash.new accepts a hash. If it's a valid hash, then it will result in a valid mash. Else you will get an error. The code in the question is a valid ruby hash.