I’ve got a class that serializes data. I may want to serialize this data as JSON, or perhaps YAML. Can I cleanly swap YAML for JSON objects in this case? I was hoping I could do something like the following. Is it a pipe dream?
FORMATS = {
:json => JSON,
:yaml => YAML,
}
def serialize(data, format)
FORMATS[format].serialize(data)
end
The code you have written should work just fine, provided that classes
JSONandYAMLboth have class method calledserialize. But I think the method that actually exists is#dump.So, you would have: