5

Suppose I have a string called very_long_string whose content I want to send to the standard output. But since the string is very long, I want to use less to display the text on the terminal. When I use

`less #{very_long_string}`

I get File not found error message, and if I use:

`less <<< #{very_long_string}`

I get unexpected redirection error message.

So, how to use less from inside Ruby?

3
  • Have you tried long_string | less ? Commented Mar 9, 2012 at 15:15
  • @alex How to do so from inside ruby? But thanks I can try `echo #{long_string}|less`. Commented Mar 9, 2012 at 15:17
  • @alex And that too doesn't seem to work, I still get file not found error message Commented Mar 9, 2012 at 15:20

3 Answers 3

19

You could open a pipe and feed your string to less via its stdin.

IO.popen("less", "w") { |f| f.puts very_long_string }

(Assuming very_long_string is the variable holding your string.)

See: http://www.ruby-doc.org/core-1.8.7/IO.html#method-c-popen

Sign up to request clarification or add additional context in comments.

1 Comment

There is more to this than meets the eye. Two issues I have: 1) hitting 'q' to quit less produces an EPIPE error that results in a nasty looking stack being dumped out. That's easy to catch wit a rescue. 2) hitting ^C before all the output has been sent to less. less basically just eats the ^C. The ruby code does get an Interrupt but I can't figure out how to gracefully exit and still have less also exit gracefully and set the TTY back to their original settings.
1

A simple hack:

require 'tempfile'
f = Tempfile.new('less')
f.write(long_string)

system("less #{f.path}")

Comments

1

Although less can read text files its natural fit is to use it as the last command in a pipe. So a natural fit would be:

shell-command-1 | shell-command-2 | shell-command-3 | less

At your shell prompt:

echo tanto va la gatta al lardo che ci lascia lo zampino|less

..So you can try this in irb:

`echo tanto va la gatta al lardo che ci lascia lo zampino|less`

but I will prefer to use:

your_string = "tanto va la gatta al lardo che ci lascia lo zampino"
`echo "#{your_string}"|less`

If you have time read this SO question.

For a thorough demonstration of using system calls in ruby see this gist: https://gist.github.com/4069

1 Comment

I actually did use it, but nothing happened. No errors, no display nothing. The script simply returned. However, IO.popen() as suggested by @user1252434 worked very well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.