Search code examples
ruby-on-railsvariable-initialization

Initialize a multiple levels hash in rails


So I have some codes look like the following:

@foo ||= {}
@foo[:bar] ||= {}
@foo[:bar][:baz] ||= {}

I am not concerning the performance, but the cleanness. Is there a more beautiful way or better way to do so?


Solution

  • {:bar => {:baz => {}}}.merge(@foo)