This question already has answers here:
What other languages have features and/or libraries similar to Perl's format?

(3 个回答)


7年前关闭。




使用 perlform ,我能够像这样格式化控制台输出:
#Print report
$~ = 'REPORT';
$^ = 'REPORT_TOP';
write ;

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

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

ruby 有没有办法做同样的事情?

编辑:

我有一个循环总结存储在散列中的每个 reportChargeCode 的 reportHours。所需的输出类似于:
Charge Code   Hours    Description
============= ======== ===============================================
CS5510        2.2575   hw13
ECE3710       5.678333 duck hunt game

最佳答案

您可以使用 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

关于ruby - ruby 中是否有类似于 perlform 的东西?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20164914/

10-16 19:30