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 408941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:44:27+00:00 2026-05-12T17:44:27+00:00

Hey there, I have read the few posts here on when/how to use the

  • 0

Hey there, I have read the few posts here on when/how to use the visitor pattern, and some articles/chapters on it, and it makes sense if you are traversing an AST and it is highly structured, and you want to encapsulate the logic into a separate “visitor” object, etc. But with Ruby, it seems like overkill because you could just use blocks to do nearly the same thing.

I would like to pretty_print xml using Nokogiri. The author recommended that I use the visitor pattern, which would require I create a FormatVisitor or something similar, so I could just say “node.accept(FormatVisitor.new)”.

The issue is, what if I want to start customizing all the stuff in the FormatVisitor (say it allows you to specify how nodes are tabbed, how attributes are sorted, how attributes are spaced, etc.).

  • One time I want the nodes to have 1 tab for each nest level, and the attributes to be in any order
  • The next time, I want the nodes to have 2 spaces, and the attributes in alphabetical order
  • The next time, I want them with 3 spaces and with two attributes per line.

I have a few options:

  • Create an options hash in the constructor (FormatVisitor.new({:tabs => 2})
  • Set values after I have constructed the Visitor
  • Subclass the FormatVisitor for each new implementation
  • Or just use blocks, not the visitor

Instead of having to construct a FormatVisitor, set values, and pass it to the node.accept method, why not just do this:


node.pretty_print do |format|
  format.tabs = 2
  format.sort_attributes_by {...}
end

That’s in contrast to what I feel like the visitor pattern would look like:


visitor = Class.new(FormatVisitor) do
  attr_accessor :format
  def pretty_print(node)
    # do something with the text
    @format.tabs = 2 # two tabs per nest level
    @format.sort_attributes_by {...}
  end
end.new
doc.children.each do |child|
  child.accept(visitor)
end

Maybe I’ve got the visitor pattern all wrong, but from what I’ve read about it in ruby, it seems like overkill. What do you think? Either way is fine with me, just wondering what how you guys feel about it.

Thanks a lot,
Lance

  • 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-05-12T17:44:28+00:00Added an answer on May 12, 2026 at 5:44 pm

    I would go with what is simple and works. I don’t know the details, but what you wrote compared with the Visitor pattern, looks simpler. If it also works for you, I would use that. Personally, I am tired with all these techniques that ask you to create a huge “network” of interelated classes, just to solve one small problem.

    Some would say, yeah, but if you do it using patterns then you can cover many future needs and blah blah. I say, do now what works and if the need arises, you can refactor in the future. In my projects, that need almost never arises, but that’s a different story.

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

Sidebar

Ask A Question

Stats

  • Questions 266k
  • Answers 266k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You would need to use the Session helper to do,… May 13, 2026 at 12:36 pm
  • Editorial Team
    Editorial Team added an answer What you are looking for is FORM Binding, there are… May 13, 2026 at 12:36 pm
  • Editorial Team
    Editorial Team added an answer $preg = '#\[((?>[^\[\]]+)|(?R))*\]#x'; May 13, 2026 at 12:36 pm

Related Questions

I have a website that I made and I am planning to redo it.
Let's say I have N > 1 TCP-based, connection-oriented (read: not a website) services
I've got some very large XML files which I read using a System.Xml.Serialization.XmlSerializer .
Hey I am writing an app in Twisted, and as it stands I have

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.