1

So I'm using a gem file to pull links from the front page of reddit. The gem function returns a hash containing ALL the links on the front page and all their info (# of comments, karma, author, date, link, ups, downs, etc). Below is a begging of the hash containg the information for the FIRST link on my front page:

{"modhash"=>"ubekfji9tr491ba98806d33ec78693dc157579335eb1ab283b", "children"=>[{"kind"=>"t3", "data"=>{"domain"=>"i.imgur.com", "banned_by"=>nil, "media_embed"=>{}, "subreddit"=>"pics", "selftext_html"=>nil, "selftext"=>"", "likes"=>nil, "link_flair_text"=>nil, "id"=>"1dt29e", "clicked"=>false, "title"=>"Our plumber in Italy put himself out there to ask us for help on his English homework :) Hope he passes his finals!", "media"=>nil, "score"=>3700, "approved_by"=>nil, "over_18"=>false, "hidden"=>false, "thumbnail"=>"http://b.thumbs.redditmedia.com/Fdid4luCfXah0bQ7.jpg", "subreddit_id"=>"t5_2qh0u", "edited"=>false, "link_flair_css_class"=>nil, "author_flair_css_class"=>nil, "downs"=>17890, "saved"=>false, "is_self"=>false, "permalink"=>"/r/pics/comments/1dt29e/our_plumber_in_italy_put_himself_out_there_to_ask/", "name"=>"t3_1dt29e", "created"=>1367893951.0, "url"=>"https://i.sstatic.net/DWZRf.jpg", "author_flair_text"=>nil, "author"=>"Junpha", "created_utc"=>1367865151.0, "distinguished"=>nil, "num_comments"=>553, "num_reports"=>nil, "ups"=>21590}},

That ending , seperates the next link which looks like this:

{"kind"=>"t3", "data"=>{"domain"=>"i.imgur.com", "banned_by"=>nil, "media_embed"=>{}, "subreddit"=>"funny", "selftext_html"=>nil, "selftext"=>"", "likes"=>nil, "link_flair_text"=>nil, "id"=>"1dt4xv", "clicked"=>false, "title"=>"What a beautiful, majestic.... oops", "media"=>nil, "score"=>2848, "approved_by"=>nil, "over_18"=>false, "hidden"=>false, "thumbnail"=>"http://a.thumbs.redditmedia.com/vkstE1PFr9iyYpux.jpg", "subreddit_id"=>"t5_2qh33", "edited"=>false, "link_flair_css_class"=>nil, "author_flair_css_class"=>nil, "downs"=>8128, "saved"=>false, "is_self"=>false, "permalink"=>"/r/funny/comments/1dt4xv/what_a_beautiful_majestic_oops/", "name"=>"t3_1dt4xv", "created"=>1367895955.0, "url"=>"https://i.sstatic.net/jzIqQ.gif", "author_flair_text"=>nil, "author"=>"themisc", "created_utc"=>1367867155.0, "distinguished"=>nil, "num_comments"=>279, "num_reports"=>nil, "ups"=>10976}},

I basically need someway to parse each entry of this hash so that I can store all the relevant stuff into an array. Really, I just want to store the "title" and "url" parts into an array. I tried something like this:

@red.get_listing().each do |x['title']|
    puts x
end

But i realized this is a hash of a hash..so I'm kinda lost as to how to parse this.

2
  • 1
    If it's a hash, why would you need to parse it? You access inner haves the same way as outer hashes. Commented May 6, 2013 at 23:44
  • @DaveNewton i'm a total newb at ruby...lol Commented May 7, 2013 at 0:42

3 Answers 3

2
hash[:children].each do |child|
  puts child[:title]
  puts child[:url]
end

Does that help?

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

2 Comments

kind of...the problem now is that the returned value from the method isn't an actual hash so you can do something like @red.get_listing()[:children].each because it says undefined method 'each'. Need a way to convert to hash now
that's probably because get_listing is not returning a hash or if it does, it needs to have indifferent access. looking at your original hash, your hash keys are strings, so you access them using strings (if they don't have indifferent access)
1
hash = {"modhash"=>"ubekfji9tr491ba98806d33ec78693dc157579335eb1ab283b", 
"children"=>[{"kind"=>"t3", "data"=>{"domain"=>"i.imgur.com", "banned_by"=>nil, "media_embed"=>{}, "subreddit"=>"pics", "selftext_html"=>nil, "selftext"=>"", "likes"=>nil, "link_flair_text"=>nil, "id"=>"1dt29e", "clicked"=>false, "title"=>"Our plumber in Italy put himself out there to ask us for help on his English homework :) Hope he passes his finals!", "media"=>nil, "score"=>3700, "approved_by"=>nil, "over_18"=>false, "hidden"=>false, "thumbnail"=>"http://b.thumbs.redditmedia.com/Fdid4luCfXah0bQ7.jpg", "subreddit_id"=>"t5_2qh0u", "edited"=>false, "link_flair_css_class"=>nil, "author_flair_css_class"=>nil, "downs"=>17890, "saved"=>false, "is_self"=>false, "permalink"=>"/r/pics/comments/1dt29e/our_plumber_in_italy_put_himself_out_there_to_ask/", "name"=>"t3_1dt29e", "created"=>1367893951.0, "url"=>"http://i.imgur.com/3xn1c8s.jpg", "author_flair_text"=>nil, "author"=>"Junpha", "created_utc"=>1367865151.0, "distinguished"=>nil, "num_comments"=>553, "num_reports"=>nil, "ups"=>21590}},{"kind"=>"t3", "data"=>{"domain"=>"i.imgur.com", "banned_by"=>nil, "media_embed"=>{}, "subreddit"=>"funny", "selftext_html"=>nil, "selftext"=>"", "likes"=>nil, "link_flair_text"=>nil, "id"=>"1dt4xv", "clicked"=>false, "title"=>"What a beautiful, majestic.... oops", "media"=>nil, "score"=>2848, "approved_by"=>nil, "over_18"=>false, "hidden"=>false, "thumbnail"=>"http://a.thumbs.redditmedia.com/vkstE1PFr9iyYpux.jpg", "subreddit_id"=>"t5_2qh33", "edited"=>false, "link_flair_css_class"=>nil, "author_flair_css_class"=>nil, "downs"=>8128, "saved"=>false, "is_self"=>false, "permalink"=>"/r/funny/comments/1dt4xv/what_a_beautiful_majestic_oops/", "name"=>"t3_1dt4xv", "created"=>1367895955.0, "url"=>"http://i.imgur.com/62q1jWN.gif", "author_flair_text"=>nil, "author"=>"themisc", "created_utc"=>1367867155.0, "distinguished"=>nil, "num_comments"=>279, "num_reports"=>nil, "ups"=>10976}}
]}


hash['children'].map { |child| child['data']['url'] }
hash['children'].map { |child| child['data']['title'] }

or if you want to use symbols (and are in rails)

hash.with_indifferent_access[:children].first[:data][:title]

Comments

0

You don't really need to parse them, just access its value:

hash[:data][:title]
hash[:data][:url]

Comments

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.