No implicit conversion of String into Integer (TypeError)?

user3137647 picture user3137647 · Dec 26, 2013 · Viewed 77.6k times · Source

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

Answer

Arup Rakshit picture Arup Rakshit · Dec 26, 2013

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.