红宝石中是否有类似于perlform的东西?

时间:2013-11-23 16:39:54

标签: ruby

使用perlform,我可以像这样格式化控制台输出:

#Print report    
$~ = 'REPORT';
$^ = 'REPORT_TOP';
write ;

# Specify format
format REPORT_TOP =
Charge Code   Hours    Description
============= ======== ===============================================
.

format REPORT =
@<<<<<<<<<<<< @<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$reportChargeCode, $reportHours, $reportDescription
.

红宝石有没有办法做同样的事情?

编辑:

我有一个循环,它总结了存储在哈希中的每个reportChargeCode的reportHours。所需的输出看起来像:

Charge Code   Hours    Description
============= ======== ===============================================
CS5510        2.2575   hw13
ECE3710       5.678333 duck hunt game

1 个答案:

答案 0 :(得分:2)

你可以使用FormatR gem:

require "formatr"
include FormatR

# Specify format
report_top = <<DOT
Charge Code   Hours    Description
============= ======== ===============================================
DOT

report = <<DOT
@<<<<<<<<<<<< @<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
reportChargeCode, reportHours, reportDescription
DOT

body_fmt = Format.new (report_top, report)

body_fmt.setPageLength(10)
num = 1

Reaports.each do |(reportChargeCode, reportHours, reportDescription)|
    body_fmt.printFormat(binding)
end