No implicit conversion of String into Integer (TypeError)?

旧时模样 提交于 2020-05-09 19:09:28

问题


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.

I'm referencing the documentation from Red Hat for the method used below:

#!/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:

[{"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 id I get this error:

no implicit conversion of String into Integer (TypeError)

Any help is appreciated


回答1:


Write as

@systemid = @getsystemid[0]['id']

Your @getsystemid is not a Hash, it is an Array of 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.



来源:https://stackoverflow.com/questions/20790499/no-implicit-conversion-of-string-into-integer-typeerror

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!