Skip to content
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

Allow input of a custom IO for print_tree #69

Open
marksiemers opened this issue Jul 21, 2018 · 1 comment
Open

Allow input of a custom IO for print_tree #69

marksiemers opened this issue Jul 21, 2018 · 1 comment
Assignees
Labels

Comments

@marksiemers
Copy link

The default is for print_tree to send output to stdout, I would like the option to send it somewhere else by passing in and IO object to it.

Currently, I'm using this workaround, but I don't really like capturing all of stdout (even if temporarily):

def fetch_tree_text(tree)
  begin
    old_stdout = $stdout
    $stdout = StringIO.new
    tree.print_tree
    $stdout.string
  ensure
    $stdout = old_stdout
  end
end

I would much rather do something like this:

def fetch_tree_text(tree)
  string_io = StringIO.new
  tree.print_tree(io: string_io)
  string_io.string
end

Or maybe even better, have a method that does the same thing, but returns a string, instead sending to an IO.

@net1957
Copy link
Contributor

net1957 commented Aug 20, 2021

Hello! print_tree accept a lambda. The default one emit a puts. but you can pass your code.

for example:

def work_on_row(node, prefix)
  # same as default result but on @io
  @io.puts "#{prefix} #{node.name}"
end

@io = StringIO.new
tree.print_tree(,, method(:work_on_row))

#do someting with @io.

Hope this help

@evolve75 evolve75 self-assigned this Jun 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants