Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何检查Httparty和Ruby中不存在的fieldw?_Ruby On Rails_Ruby_Http_Null_Httparty - Fatal编程技术网

Ruby on rails 如何检查Httparty和Ruby中不存在的fieldw?

Ruby on rails 如何检查Httparty和Ruby中不存在的fieldw?,ruby-on-rails,ruby,http,null,httparty,Ruby On Rails,Ruby,Http,Null,Httparty,是HttpParty请求的xml结果 <people> <person> <height>192</height> <weight>80</weight> </person> <person>

是HttpParty请求的xml结果

<people>
                <person>
                    <height>192</height>
                    <weight>80</weight>
                </person>
                <person>
                    <height>180</height>
                </person>
</people>

当它找到第一个没有登记体重的人时,它就断了。我尝试过使用
.nil?
,但运气不佳。如何识别不存在的字段?

我认为项是一个散列。所以你可以做一些事情

person = item["person"]
if person.key?("weight")
  puts person["weight"] 
else
  puts "no weight"
end


我建议你学习Ruby哈希

是的,到目前为止Ruby还不是我的主要编码语言
person = item["person"]
if person.key?("weight")
  puts person["weight"] 
else
  puts "no weight"
end
person = item["person"]
puts person.fetch("weight", "no weight")