2015-09-14 68 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