1

I have a variable (result) that looks like this when doing YAML::dump(result):

responseHeader: 
  status: 0
  QTime: 1
  params: 
    wt: ruby
    q: enid:(15542697739)
response: 
  numFound: 1
  start: 0
  docs: 
  - enid: "15542697739"

I want to do a conditional comparison on the enid like this:

if result["response"]["docs"]["enid"].to_i == num['1']['car']

where num['1']['car'] is an integer.

Whenever I attempt this, I get a TypeError thrown,

can't convert String into Integer
(TypeError)

even if I attempt

result["response"]["docs"]["enid"].to_i

or

Integer(result["response"]["docs"]["enid"])

How do I get my enid value converted to an integer so I can make this comparison?

1 Answer 1

2

The problem is that what's in result["response"]["docs"] is NOT a hash and you're addressing it like one. What you need in this case is result["response"]["docs"][0]["enid"]. If you want to see why, try p result["response"] to see what Ruby data structures are being used at each level. YAML can be a little misleading here even if you've been reading it a while.

Sign up to request clarification or add additional context in comments.

1 Comment

Great! Thanks Peter! Just right. Tried with the [0] term and that works. Hope this helps others as well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.