ruby等同于这个python脚本会是什么?

时间:2015-05-14 00:41:45

标签: python ruby-on-rails ruby

for (int i = 0; i < (myArray.length - 1); i++) {
    for (int j = 0; j < myArray.length - i - 1; j++) {
        if (myArray[j] > myArray[j + 1]) {
            char swap = myArray[j];
            myArray[j] = myArray[j + 1];
            myArray[j + 1] = swap;
        }
    }
}

它只是从数组打印输出而是打印成行。我怎么能用ruby来反映这个?

1 个答案:

答案 0 :(得分:1)

的Python:

print ("{0:5s} {1:7s} {2:9s} {3:6s} {4:25s} {5:s}".format('Rank', 'Points', 'Comments', 'Hours', 'Sub', 'Link'))

红宝石:

puts "%-5s %-7s %-9s %-6s %-25s %-5s" % ['Rank', 'Points', 'Comments', 'Hours', 'Sub', 'Link']

可替换地:

puts sprintf("%-5s %-7s %-9s %-6s %-25s %-5s", *['Rank', 'Points', 'Comments', 'Hours', 'Sub', 'Link'])