以下按预期工作:
require 'json'
hash = {}
hash['some_key'] = false
hash['another_key'] = "that"
hash['my_key'] = hash.delete('some_key')
puts hash.to_json
结果
{"another_key":"that","my_key":false}
但是,当我尝试以下操作时,新键的值为空。另外,旧密钥不会被删除:
require 'whois-parser'
require 'json'
c = Whois::Client.new
record = c.lookup('google.com')
parser = record.parser
msg = {}
for prop in Whois::Parser::PROPERTIES
msg[prop] = parser.send(prop)
end
msg['available'] = msg.delete('available?')
puts msg.to_json
结果:
{
"disclaimer":"TERMS OF USE: ...",
"domain":"google.com",
"domain_id":"2138514_DOMAIN_COM-VRSN",
"status":"registered",
"available?":false,
"registered?":true,
"created_on":"1997-09-15 00:00:00 -0700",
"updated_on":"2018-02-21 10:45:07 -0800",
"expires_on":"2020-09-13 21:00:00 -0700",
"registrar":"#<struct Whois::Parser::Registrar id=\"292\", name=\"MarkMonitor, Inc.\", organization=\"MarkMonitor, Inc.\", url=\"http://www.markmonitor.com\">",
"registrant_contacts":[],
"admin_contacts":[],
"technical_contacts":[],
"nameservers":["ns2.google.com","ns1.google.com","ns4.google.com","ns3.google.com"],
"available":null
}
为什么键available?
仍被设置,为什么键available
的值是null
?
答案 0 :(得分:1)
正如@mbuechmann所说,Whois::Parser::PROPERTIES
是一个符号数组。这意味着您的msg
哈希具有符号键,您不能将它们作为字符串访问。
msg = {}
# it is more common way for iteration in ruby
Whois::Parser::PROPERTIES.each do |prop|
msg[prop] = parser.send(prop)
end
msg[:available] = msg.delete(:available?)
# don't convert msg to json to check real keys format
puts msg