以下代码尝试检测 a)replica_2
具有键:primary
,而 b)replica_1
不具有该键或具有true
它的值。我无法检测哈希是否有密钥。我正在寻找评论,更正等。
replica_1 = { :hostname => "16.86.193.100", :port => 27017 }
replica_2 = { :hostname => "16.86.193.102", :port => 27019, :primary => true }
puts "replica_1=", replica_1
if replica_1[ :primary ] = true
puts "replica_1[ :primary ] is set"
else
puts "replica_1[ :primary ] is not set/true"
end
puts "replica_2=", replica_2
if replica_2[ :primary ] = true
puts "replica_2[ :primary ] is set"
end
输出:
replica_1=
{:hostname=>"16.86.193.100", :port=>27017}
replica_1[ :primary ] is set
replica_2=
{:hostname=>"16.86.193.102", :port=>27019, :primary=>true}
replica_2[ :primary ] is set