Ruby 字符串输出

Ruby String Output

我的目标是创建一个包含 3 个不同变量(所有字符串)的单行输出,其格式为第一个变量左对齐,第二个变量居中,最后一个变量右对齐 spaces 填充变量,如果它们的大小小于分配的 space。

变量槽是: 变量 1,3 = 最大字符串长度为 10 变量 2 = 最大字符串长度为 7 (在变量 1&2 和 2&3 之间将有 space 个 2 space 的缓冲区)

无论用户输入如何,我都希望变量 1 和 3 的大小相同 (10)。如果输入小于 10,我希望在末尾(变量 1 - 左对齐)或开头(变量 3 - 右对齐)进行填充。

我已经尝试 using/learning 具有各种格式和参数的 sprintf 方法,但我总是收到错误或空白输出。

****IDEAL OUTPUT EXAMPLE****

VARIABLE 1  VARIAB2  VARIABLE 3

您可以使用 String class 中可用的各种方法。

input1 = 'one'
input2 = 'two'
input3 = 'three'

input1 = input1.ljust 10
 #=> "one       "
input2 = input2.center 7     #credit to user Simple Lime
 #=> "  two  "
input3 = input3.rjust 10
 #=> "     three"

puts output = input1 + '  ' + input2 + '  ' + input3
 #one           two         three
output.size
 #=> 31