1. 终端下进度条
https://github.com/jfelchner/ruby-progressbar
https://github.com/paul/progress_bar
2个progressbar都不错,第一个在git上关注的人比较多,使用起来都比较简单.
2. 终端下表格
https://github.com/visionmedia/terminal-table
https://github.com/aptinio/text-table
text-table比较新,作者也是受terminal-table启发,个人比较喜欢text-table,因为to_table方法,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
require 'rubygems' require 'text-table' array = [ ['Student', 'Mid-Terms', 'Finals'], ['Sam', 94, 93], ['Jane', 92, 99], ['Average', 93, 96] ] puts array.to_table # +---------+-----------+--------+ # | Student | Mid-Terms | Finals | # | Sam | 94 | 93 | # | Jane | 92 | 99 | # | Average | 93 | 96 | # +---------+-----------+--------+ |
3. PrettyPrint
https://github.com/michaeldv/awesome_print
ruby默认的pp模块,能够很优雅的输出变量结构。无意中发现了ap,比pp输出更清晰和漂亮。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$ cat > 1.rb require "awesome_print" data = [ false, 42, %w(forty two), { :now => Time.now, :class => Time.now.class, :distance => 42e42 } ] ap data ^D $ ruby 1.rb [ [0] false, [1] 42, [2] [ [0] "forty", [1] "two" ], [3] { :class => Time < Object, :now => Fri Apr 02 19:55:53 -0700 2010, :distance => 4.2e+43 } ] |
4. daemon程序
ruby 1.9.x可以使用Process.daemon,将程序变成daemon。不过个人还是比较喜欢daemons这个gem.它提供3种方法将程序daemon,而且都比较简单。提供了start、run(前台运行)、stop、restart等启停命令,而且还提供类supervisor的功能,当程序down掉后可以自动重启。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
require 'rubygems' require 'daemons' options = { :backtrace => true, #默认后台运行 :dir_mode => :script, :dir => 'pids', #写pid文件 :monitor => true #启动监控进程 } current_dir = Dir.pwd #server-name表示进程的名字,ps可以看到2个进程server-name和server-name-monitor Daemons.run_proc('server-name', options) do loop do Dir.chdir(current_dir) #daemons默认会切换目录到,所以需要chdir run end end |
5. 字符彩色输出
1 2 3 4 |
require 'colorize' puts "I am now light red.".light_red puts "I am now light blue.".light_blue puts "I am a super coder".yellow |