Skip to content

Instantly share code, notes, and snippets.

@zdman135
Created April 30, 2015 17:18
Show Gist options
  • Save zdman135/23960f995685739a55b8 to your computer and use it in GitHub Desktop.
Save zdman135/23960f995685739a55b8 to your computer and use it in GitHub Desktop.
@array[0]['elements'][0]['comments'][0]['value'] = 'a string'
i=0
while @array[0]['elements'][0]['comments'][0] != nil
name = @array[0]['elements'][0]['comments'][i]['value'] #put into hash where name is key and it equals the value
i += 1
end
@IceDragon200
Copy link

If your dealing with a particular sub-node, and only that sub-node of a hash, you should probably store as a variable to remove the noise and duplication.

@array[0]['elements'][0]['comments'][0]['value'] = 'a string'

element = @array[0]['elements'][0]
comments = element['comments']
i=0
# I'm assuming your suppose to be looping and checking each comment, until it becomes nil
# you can also store the comment from the index
while (comment = comments[i]) != nil
  name = comment['value'] #put into hash where name is key and it equals the value
  i += 1
end

Another alternative is to use comments.compact.reject(&:nil?), unless your using nil as a terminator (in which case, wat)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment