Things to consider when paging your program's output

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.

Last Updated: 2022-05-26

Say I want to hard-code that my program's output is to be paged via the more command. (i.e. do this in Ruby etc. instead of ad-hoc on the command line)

Then I need to:

if $stdout.tty?
  # Take stock of this way of opening a write pipe!
  pager = open("|more", "w")
  # Be careful to close pipe
  at_exit { pager.close }
  $stdout = pager
puts text