ruby 没有将字符串隐式转换为整数(TypeError)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20790499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No implicit conversion of String into Integer (TypeError)?
提问by user3137647
I'm trying to write a script that will get a system ID from Red Hat Satellite/Spacewalk, which uses XMLRPC. I'm trying to get the ID which is the first value when using the XMLRPC client using the system name.
我正在尝试编写一个脚本,该脚本将从使用 XMLRPC 的 Red Hat Satellite/Spacewalk 获取系统 ID。我正在尝试使用系统名称获取使用 XMLRPC 客户端时的第一个值的 ID。
I'm referencing the documentationfrom Red Hat for the method used below:
我正在参考Red Hat的文档以了解以下使用的方法:
#!/usr/bin/env ruby
require "xmlrpc/client"
@SATELLITE_URL = "satellite.rdu.salab.redhat.com"
@SATELLITE_API = "/rpc/api"
@SATELLITE_LOGIN = "********"
@SATELLITE_PASSWORD = "*******"
@client = XMLRPC::Client.new(@SATELLITE_URL, @SATELLITE_API)
@key = @client.call("auth.login", @SATELLITE_LOGIN, @SATELLITE_PASSWORD)
@getsystemid = @client.call("system.getId", @key, 'cfme038')
print "#{@getsystemid}"
@systemid = @getsystemid ['id']
The output of getsystemid looks like this:
getsystemid 的输出如下所示:
[{"id"=>1000010466, "name"=>"cfme038", "last_checkin"=>#<XMLRPC::DateTime:0x007f9581042428 @year=2013, @month=12, @day=26, @hour=14, @min=31, @sec=28>}]
But when I try to just get just idI get this error:
但是,当我尝试只获取时,id我收到此错误:
no implicit conversion of String into Integer (TypeError)
Any help is appreciated
任何帮助表示赞赏
回答by Arup Rakshit
Write as
写成
@systemid = @getsystemid[0]['id']
Your @getsystemidis not a Hash, it is an Arrayof Hash. @getsystemid[0]will give you the intended hash {"id"=>1000010466, "name"=>"cfme038", "last_checkin"=>#}. Now you can use Hash#[]method to access the value of the hash by using its keys.
你@getsystemid是不是Hash,它是一个Array的Hash。@getsystemid[0]会给你预期的哈希值{"id"=>1000010466, "name"=>"cfme038", "last_checkin"=>#}。现在您可以使用Hash#[]方法通过使用其键来访问散列的值。

