YAML - TypeError: can't dump anonymous module

delayed-job, ruby-on-rails, yaml

Solution

The problem is, that the request.env hash has a lot of nested objects (and especially modules), which can't be converted to yaml. The trick is to delete those parts of the hash, that can't be converted.

tmp_env = request.env.clone
tmp_env.delete "action_dispatch.routes"
tmp_env.delete "action_controller.instance"
tmp_env["action_dispatch.remote_ip"] = tmp_env["action_dispatch.remote_ip"].to_s 
p tmp_env.to_yaml # now it works

We first clone the original `env` hash, to not accidentally modify it. Then we delete those keys from our copy, which cause errors.

`tmp_env["action_dispatch.routes"]` contains a reference to an unnamed module within a `ActionDispatch::Routing::RouteSet` object, which is the cause of your error. We better delete it.

`tmp_env["action_controller.instance"]` contains a reference to the original `env`-hash (which we cannot convert). Delete it.

And finally `tmp_env["action_dispatch.remote_ip"]` looks like a string (when inspecting it), but it is a `ActionDispatch::RemoteIp::GetIp` instance. It contains another reference to the original `env` hash. We convert it to a string, because I don't know if your are interested in that key later.

Additionally, you may remove many more keys to reduce the size of your yaml output. However, this should work without throwing the error you experienced. A leaner solution would be to start with an empty Hash and only copy the keys you really need in your yaml output.

Tested with ruby 1.9.3 and rails 3.2.13

Problem

In an action of application_controller, if we try: ``` p request.env.to_yaml ``` I will got this error: ``` TypeError: can't dump anonymous module: #<Module:0x007fee26e34ad8> from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:267:in `visit_Module' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:102:in `accept' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:447:in `block in dump_ivars' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:445:in `each' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:445:in `dump_ivars' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:124:in `visit_Object' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:102:in `accept' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:447:in `block in dump_ivars' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:445:in `each' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:445:in `dump_ivars' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:124:in `visit_Object' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:102:in `accept' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:292:in `block in visit_Hash' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:290:in `each' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:290:in `visit_Hash' from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:102:in `accept' ``` My question is: how can I serialize `request.env` to yaml? Actually, I was supposed to pass request.env to delayed_job and send out email, and I got this error because delayed_job need serializing object into DB.

Original source