Flattening nested hash to a single hash with Ruby/Rails

You could do this: def flatten_hash(hash) hash.each_with_object({}) do |(k, v), h| if v.is_a? Hash flatten_hash(v).map do |h_k, h_v| h[“#{k}.#{h_k}”.to_sym] = h_v end else h[k] = v end end end flatten_hash(:foo => “bar”, :hello => { :world => “Hello World”, :bro => “What’s up dude?”, }, :a => { :b => { :c => “d” } … Read more

Could not write JSON: Infinite recursion (StackOverflowError); nested exception spring boot

You are facing this issue because the Statemaster model contains the object of Districtmaster model, which itself contains the object of Statemaster model. This causes an infinite json recursion. You can solve this issue by 3 methods. 1 – Create a DTO and include only the fields that you want to display in the response. … Read more

Prevent recursive trigger in PostgreSQL

This is what I do in PostgreSQL 9.2, although I must admit I did not find this approach documented. There is a function pg_trigger_depth() documented here, which I use to differentiate between original and nested calls in the trigger. CREATE TRIGGER trg_taxonomic_positions AFTER INSERT OR UPDATE OF taxonomic_position ON taxon_concepts FOR EACH ROW WHEN (pg_trigger_depth() … Read more

Examples of Recursive functions [closed]

This illustration is in English, rather than an actual programming language, but is useful for explaining the process in a non-technical way: A child couldn’t sleep, so her mother told a story about a little frog, who couldn’t sleep, so the frog’s mother told a story about a little bear, who couldn’t sleep, so bear’s … Read more

Recursive call – Action lambda

Just define the delegate Action and assign null to it before calling it recursively. Action<IItem, Int32> recurse = null; Then recurse = new Action<IItem, Int32>((item, depth ) => { if (item.Items.Count() > 0) recurse(item, depth + 1); // red squiggly here // … }); Good luck!