1

I'm getting a surprising error. I check if one record's ID matches the ID of another object:

new_post = posts_with_score.detect {|post| post["id"].to_s == original_post.id.to_s }

I get an error TypeError (no implicit conversion of String into Integer). This is surprising since both id's are explicitly converted into strings.

What am I not seeing here? Why is this equality check trying to convert into integers?

Edit:

Here is what post inside the detect looks like: {"time_since_post_in_hours"=>15.810972532939815, "upvote_count"=>324, "id"=>1, "score"=>4.3103601541380465}

9
  • 2
    Are you sure you are getting the error from this particular line ? Commented Aug 6, 2014 at 5:36
  • Yes, was double checking that and it definitely points to this Line number in a particular function. There are no other checks or conversions anywhere nearby either. Commented Aug 6, 2014 at 5:36
  • What happens when you do puts post.class inside your block? Commented Aug 6, 2014 at 5:37
  • 1
    i think post inside the block is an array. the error is about converting a string into integer, not the other way around. Commented Aug 6, 2014 at 5:41
  • 1
    given the edit, expand detect to use multiple lines. place one line as post['id'], another as original_post.id, then your conditional as the last line. you should be able to pinpoint the cause of the issue. Commented Aug 6, 2014 at 5:52

1 Answer 1

1

Because of the way your hash is structured, when you iterate through it, post is an array. So when you do post["id"] Ruby tries to convert your index (a string) into an Integer. That's when you get the error. Notice that the error is "no implicit conversion of String into Integer" and not the other way around. So check your hash structure to adjust to what you want.

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

2 Comments

Just edited the question since I was checking if Post was an array, it looks like Post is still a hash
Would you mind also posting the content of posts_with_score ?

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.