Forum: Ruby clear screen

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
10672facba91d4fd1cc51b3a81071ec5?d=identicon&s=25 Christian (Guest)
on 2007-04-26 08:27
(Received via mailing list)
Is there a way to clear the terminal screen by using Ruby directly?
Instead of using system("clear").

Cheers
Christian
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2007-04-26 09:20
(Received via mailing list)
On Apr 26, 2007, at 8:25 AM, Christian wrote:

> Is there a way to clear the terminal screen by using Ruby directly?
> Instead of using system("clear").

Nothing builtin. A lighter approach is to print an ANSI escape
sequence if your terminal understands it:

   module Screen
     def self.clear
       print "\e[2J\e[f"
     end
   end

-- fxn
Caf38c89d40443a858741b61ac6d82de?d=identicon&s=25 Dan Zwell (Guest)
on 2007-04-26 09:31
(Received via mailing list)
Christian,

The following is a hack: I checked, and on my system, "clear" sends the
following control characters to the screen: ^[[H^[[2J

This will only work on unix, and I'm not sure whether it works on all
terminals, but

puts "\e[H\e[2J"

clears the screen. You might want to use "if $stdout.isatty" so that you
don't get control characters in an output file...

Note that this is pretty much exactly the same thing as system("clear").
If that's not good enough, you probably want to look at terminal
libraries--I think ruby-termios might be useful, but I have no
experience with it.

Dan
10672facba91d4fd1cc51b3a81071ec5?d=identicon&s=25 Christian (Guest)
on 2007-04-26 16:00
(Received via mailing list)
Thanks a lot. I will stick to the escape characters because I also
need to set the cursor position.

Cheers
Christian
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2007-04-26 16:13
(Received via mailing list)
On Apr 26, 2007, at 10:00 AM, Christian wrote:

> Thanks a lot. I will stick to the escape characters because I also
> need to set the cursor position.

You may want to check out the Ruby ncurses project:
http://ncurses-ruby.berlios.de/

Gary Wright
This topic is locked and can not be replied to.