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.
Christian (Guest)
on 2007-04-26 10: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
Xavier N. (Guest)
on 2007-04-26 11: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
Dan Z. (Guest)
on 2007-04-26 11: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
Christian (Guest)
on 2007-04-26 18: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
Gary W. (Guest)
on 2007-04-26 18: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 W.
This topic is locked and can not be replied to.