ruby - How do I convert a hash's values into lambdas recursively? -


i have hash looks this:

     {        :a => "700",        :b => "600",        :c => "500",        :d => "400",        :e => "300",        :f => {          :g => "200",          :h => [            "test"          ]        }     } 

my goal iterate on hash , return copy have values wrapped in lambda, similar this: https://github.com/thoughtbot/paperclip/blob/dca87ec5d8038b2d436a75ad6119c8eb67b73e70/spec/paperclip/style_spec.rb#l44

i went each_with_object({}) best can wrap first level, tried check when meet hash in cycle (:f in case, it's key's values should lambda unless hash well) , treat it, it's becoming quite troublesome.

def hash_values_to_lambda(old_hash)   {}.tap |new_hash|     old_hash.each |key, value|       new_hash[key] =         if value.is_a?(hash)           hash_values_to_lambda(value)         else           lambda { value } # or -> { value } new syntax         end     end   end end 

if want, can go each_with_object instead of tap:

old_hash.each_with_object({}) |(key, value), new_hash|   # else remains same end 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -