Your Ad Here

Posted By

indianocean on 03/15/07


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

maxkks
webstic


Extracting all keys from a multi-dimensional hash


 / Published in: Ruby
 

URL: http://snippets.dzone.com/posts/show/1908

Check comments

  1. class Hash
  2. def extract_keys(keys = [], temp = [])
  3. temp = temp.dup
  4. temp2 = temp.dup
  5. self.each { |key, value|
  6. if value.class != Hash
  7. temp << key
  8. keys << temp
  9. #puts temp.inspect
  10. temp = []
  11. end
  12. }
  13. temp = temp2
  14. self.each { |key, value|
  15. if value.class == Hash
  16. temp << key
  17. keys = value.extract_keys(keys, temp)
  18. end
  19. }
  20.  
  21. keys
  22. end
  23. end

Report this snippet  

You need to login to post a comment.