Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:17:41+00:00 2026-06-12T02:17:41+00:00

I have this old code using Sych doing : yaml_as tag:yaml.org,2002:#{self} def to_yaml(opts =

  • 0

I have this old code using Sych doing :

yaml_as "tag:yaml.org,2002:#{self}"
def to_yaml(opts = {})
  YAML::quick_emit(self, opts) do |out|
    out.map(taguri, to_yaml_style) do |map|
      map.add('name', name)
      map.add('address', full_address.upcase) if full_address?
    end
  end
end

which outputs something like that :

--- !Contact
name: SMOKE OIL
address: |-
  SMOKE OIL
  1 RUE DE LA PAIX
  75002 PARIS
  FRANCE

Now, I’m upgrading that old code, going to Psych so, I read the doc and did :

yaml_as "tag:yaml.org,2002:#{self}"
def encode_with(coder)
  coder['name'] = name
  coder['address'] = full_address.upcase if full_address?
end

And that does :

--- !Contact
name: SMOKE OIL
address: ! "SMOKE OIL\n1 RUE DE LA PAIX\n75002 PARIS\nFRANCE"

It’s nice YAML but, it’s supposed to be the output of a whois server, and it’s way less readable by humans…

So, I went back to the doc, and looked at the second way of doing things, that is, building an AST. Now, unless I’m not seeing something, nothing explains you how to take the AST you built, and plug it in a way Psych.dump(obj) would still work…

I tried doing (without much hope) :

a = Psych::Nodes::Scalar(full_address.upcase)
a.style = Psych::Nodes::LITTERAL
coder['address'] = a if full_address?

but, obviously, it did not do what I hoped it’d do… I also tried :

def encode_with(coder)
  Psych::Nodes::Mapping.new.tap do |map|
    map.children << Psych::Nodes::Scalar.new("name")
    map.children << Psych::Nodes::Scalar.new(name)
    map.children << Psych::Nodes::Scalar.new("address")
    a = Psych::Nodes::Scalar.new(full_address.upcase)
    a.style = 4
    map.children << a
  end
end

But, I could not see how to plug it into the coder…

Also, the answer needs to work when doing recursive things, this is a Contact objet, but one can ask for a Domain which will contain a few contacts and I want it as DRY as possible 🙂

So, anyone has a hint on how to do this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:17:43+00:00Added an answer on June 12, 2026 at 2:17 am

    If you want to create your own AST then you can’t use Psych.dump. Psych.dump creates its own AST using the Psych defaults. In your case you want to customise the AST creation process.

    Looking at the source of Psych.dump you can see it uses a Psych::Visitors::YAMLTree to create the AST. You can subclass this and customise how it handles your Contact class to get the output you want. In particular you need to override the accept method.

    Here’s a simple example that just special cases the Contact class:

    class MyYAMLTree < Psych::Visitors::YAMLTree
      def accept target
        return super unless target.is_a? Contact
    
        @emitter.start_mapping(nil, "tag:yaml.org,2002:#{target.class}", false, Psych::Nodes::Mapping::BLOCK)
    
        @emitter.scalar 'name', nil, nil, true, false, Psych::Nodes::Scalar::ANY
        @emitter.scalar target.name, nil, nil, true, false, Psych::Nodes::Scalar::ANY
    
        @emitter.scalar 'address', nil, nil, true, false, Psych::Nodes::Scalar::ANY
    
        #this is the where we make the address string a literal
        @emitter.scalar target.full_address, nil, nil, true, false, Psych::Nodes::Scalar::LITERAL
    
        @emitter.end_mapping
      end
    end
    

    Note that it’s the Psych::Visitors::YAMLTree class that calls encode_with, this will bypass it altogether for your class.

    In order to use this, use something like (this is basically a simplified version of Psych.dump using MyYAMLTree):

    def my_yaml o
      visitor = MyYAMLTree.new
      visitor << o
      visitor.tree.yaml
    end
    

    This is obviously just a simple example, but hopefully it’ll point you in the right direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a short question i have wrote this in java. Old code: class
We have some code kicking around that uses this old internal Sun package for
im working on a old code and i have this warning message: Passed-by-value struct
I have this legacy code which embeds an SWF into an HTML using an
I have been doing some tests (to replace old code) with the __invoke magic
We have been using Microsoft Enterprise Lib for data access, some old legacy code
I have some old code like this: private int ParseByte(byte theByte) { byte[] bytes
I am using suds client for WSDL in our project. i have this code
I have old c++ code using strstream and using pcount and freeze methods of
I have this js code where I am using the Mapstraction library to show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.