psych icon indicating copy to clipboard operation
psych copied to clipboard

Working with Tags

Open trans opened this issue 14 years ago • 0 comments

I noticed today that an undefined tag appears to be simply thrown away.

    o = YAML.load('--- !foo "something"')  #=> "something"
    o.class  #=> String

I actually expected it to raise an error, and was somewhat pleased that it did not. But without the tag it's not much use in any case. What would be useful is a YAML wrapper around the object that delegates to it, but also provides a #tag method (and anything else that YAML might need), something like:

    o = YAML.load('--- !foo "something"')  #=> <!foo "something">
    o.tag #=> "!foo"
    o.to_obj #=> "something"

This would make working with YAML documents that use tags much easier, b/c it would mean handlers would not have to be defined upfront and with necessary pre-knowledge. Rather they could be handled on the fly and even dynamically if desired.

I've been thinking about this for some time as I routinely find myself avoiding any use of YAML tags simply b/c of the cumbersome nature of handling them.

trans avatar Jul 28 '11 21:07 trans