views:

46

answers:

3

I'm not a Ruby guy, I just play one on television. I have to modify someone's old Cron job to pull down some JSON and convert it into objects.

Here's the code

raw_json = Net::HTTP.get(URI.parse("url removed to protect the innocent"))

tags = ActiveSupport::JSON.decode(raw_json)

puts tags.count

tags.count will accurately trace as 5, but THAT LINE immediately causes a crash as follows:

5 #the accurate count!
rake aborted!
undefined method `count' for false:FalseClass

What is the dealio?

+1  A: 

so I have no idea what is going on here, but JSON.decode should give you a hash, which doesn't have a count method. It does have a size method though

tags.size

if that doesn't work, try doing p tags, or puts tags.class.name to try and figure out what you are working with

Matt Briggs
actually tags.size and tags.count both return the correct number, 5, go figure. puts tags.class.name prints out as "Array" - so there you have it.
Jasconius
A: 

Apparently tags is false , which may mean that your Net::HTTP.get failed (I guess your URL is wrong). Try to print tags to see what it is. (I guess anyway, that you should use a valid URI)

mb14
+1  A: 

What is the contents of raw_json? What appears to be happening is that ActiveSupport::JSON#decode is returning false (hence undefined method 'count' for false:FalseClass). I think JSON#decode only returns false when given an empty string, which would mean HTTP#get is returning an empty string. Check on raw_json and see if it contains what you expect.

Jordan
But it's not returning false because it's reporting an accurate collection count... how could "False" return 5? And raw_json is absolutely correct and has the content I am expecting.
Jasconius