2015-09-14 67 views
0

我刚刚开始使用RoR并修改现有程序。使用Ruby 2.2.2p95,Rails 4.1.13.rc1和Prawn 2.0.1 文本处于有界框中。在RoR文本输出中右对齐数字

pdf.column_box([0,pdf.cursor], :columns => 2, :width ==> pdf.bounds.width 

person_text << "\n#{sprintf "%20d", count} #{'Grandchild'.pluralize(count)}" 

如果count = 234,那么输出是:

234 Grandchildren 

如果count = 4238,那么输出是:

4238 Grandchildren 

没有填补空白的右对齐的数字一个20个空间场。输出应如下所示:

  234 Grandchildren 
      4238 Grandchildren 

什么是错? 如何编码?

+0

问题与大虾。虾在前后剥去白色空间。输出位于左边缘并带有前导空白。 –

回答

0

String类有两个方法称为ljustrjust,可以帮助你

2.2.2 :010 > str=123 
=> 123 
2.2.2 :011 > "#{str.to_s.ljust(5)} Grandchildren" 
=> "123 Grandchildren" 
2.2.2 :012 > str=1234 
=> 1234 
2.2.2 :013 > "#{str.to_s.ljust(5)} Grandchildren" 
=> "1234 Grandchildren" 
2.2.2 :014 > "#{"123".to_s.ljust(5)} Grandchildren" 
=> "123 Grandchildren" 
2.2.2 :015 > 

如果你正在做的,你可以可以通过CSS来做到这一点的网页。

+0

感谢您的建议。这不适用于虾。这个解决方案的领先空白被Prawn剥离。 –

+0

你可以用空行创建一个表吗? –

0

由于白虾剥皮,在第一代中加入一片牛奶。 POS机。

> count = 4235 
> srt = "#{sprintf("%20d", count)}" 
> string = srt.gsub(/ (?=)/, Prawn::Text::NBSP) 
> person_text << "\n#{string} #{'Child'.pluralize(count)}" 

    =>     4235 Children