Advertisement
t_a_w

Hash#leaf_values

Jun 26th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.23 KB | None | 0 0
  1. class Hash
  2.   def leaf_values
  3.     flat_map do |k,v|
  4.       if v.is_a?(Hash)
  5.         v.leaf_values
  6.       else
  7.         [v]
  8.       end
  9.     end
  10.   end
  11. end
  12.  
  13. p({"root"=>{:a=>"tom", :b=>{:c => 1, :x => 2}}}.leaf_values) # => ["tom", 1, 2]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement