-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
to_yaml raises a NoMethodError when used with a SimpleDelegator #100
Comments
having the exact same issue with delayed_job_active_record by using handle_asynchronously to queue jobs https://github.com/collectiveidea/delayed_job#queuing-jobs |
A hackish workaround: # note the resulting encoding is a bit ugly
class Delegator
def encode_with coder
ivars = instance_variables.reject {|var| /\A@delegate_/ =~ var}
coder['obj'] = __getobj__
unless ivars.empty?
coder['ivars'] = Hash[ivars.map{|var| [var[1..-1], instance_variable_get(var)]}]
end
end
def init_with coder
(coder['ivars'] || {}).each do |k, v|
instance_variable_set :"@#{k}", v
end
__setobj__ coder['obj']
end
end |
Same error if trying to serialize params received from multipart form submission.
|
Same error when you try to dump response from HTTParty: require 'httparty'
require 'yaml'
module FQL
include HTTParty
base_uri 'http://graph.facebook.com'
def self.link_stat url
self.get("/fql", {
:query => {
:q => <<-FBQL.squeeze(' ').strip
select total_count
from link_stat
where url=#{url.inspect}
FBQL
}
})
end
end
YAML.dump FQL.link_stat "http://www.example.com" |
Maybe psych should define @dispatch_cache = Hash.new do |h,klass|
method = "visit_#{(klass.name || '').split('::').join('_')}"
method = respond_to?(method) ? method : h[klass.superclass]
raise(TypeError, "Can't dump #{target.class}") unless method
h[klass] = method
end because it traverses up the class hierarchy expecting to encounter a class for which |
Defining a serialization mechanism for BasicObject doesn't make sense for solving a problem with a DelegateObject. We can add a custom serialization mechanism for |
@tenderlove I agree; these are probably two separate problems: (1) can't serialize SimpleDelegator-based objects, and (2) can't serialize objects that derive from BasicObject. For the latter, there are a couple of reasonable approaches:
|
Well, Marshal seems to be able to dump SimpleDelegator based objects, so I think Psych should support it. Marshal won't dump BasicObjects though. |
👍 👏 ❤️ |
ruby has built-in psych but it's old on some rubies and it has this issue there: ruby/psych#100
The following code:
The last line raises an error:
I've looked at the source for yaml_tree.rb around line 26 and it goes:
Is this an edge-case interaction between SimpleDelegator and Psych YAML engine? Neither
foo.to_yaml
nordelegate.to_yaml
work properly—it seems that only when another object contains a reference to a SimpleDelegator doesto_yaml
fail.The text was updated successfully, but these errors were encountered: