loops - Ruby deep hash looping? -
i have big nested hash in form below. need loop through , pull out name , url of each repository, can't seem that. suggestions?
code snippet:
repo_json = get_touched_repos() repo_hash = json.parse(repo_json) puts repo_hash.class puts repo_hash['repositories'][0]['name']
the hash:
{ "repositories": [ { "type": "repo", "username": "...", "name": "....", "owner": "...", "homepage": "", "description": "description", "language": "java", "watchers": 2, "followers": 2, "forks": 1, "size": "open_issues": 0, "score": 1.0, "has_downloads": true, "has_issues": true, "has_wiki": true, "fork": false, "private": false, "url": "http://my.domain.com/repo/name", "created": "2012-07-02t17:47:54z", "created_at": "2012-07-02t17:47:54z", "pushed_at": "2014-03-20t20:09:38z", "pushed": "2014-03-20t20:09:38z" }, {....} ] }
you can utilize array#each method this
repo_hash['repositories'].each |repo| puts repo['name'] puts repo['url'] end
ruby loops hash
No comments:
Post a Comment