它是一个有效的Ruby Mash语法吗?

时间:2016-08-08 09:21:24

标签: ruby

我在自述https://github.com/hw-cookbooks/haproxy的自述文件中找到了以下mash:

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

我想知道下面的Mash表示的内容:

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

是哈希数组吗?

1 个答案:

答案 0 :(得分:2)

这是有效的ruby语法,是的,这将生成一个包含一个元素的数组,这是一个哈希。

[
  "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在这里完全无关紧要。 Mash.new接受哈希。如果它是有效的哈希值,那么它将产生有效的混搭。否则你会收到错误。问题中的代码是有效的ruby散列。