没有整数的Ruby字符串重复

时间:2014-10-27 15:13:49

标签: ruby string repeat

我刚刚在http://repl.it/2Om/6找到CodeGolf个答案:

puts"The eight#{e="een-hundreds were a time for "}rum.
The ninet#{e}fun.
The two-thousands are a time to run
a civilized classroom.
"*(?X-??)

原帖:https://codegolf.stackexchange.com/a/40250

我很好奇,它是如何运作的?我以前从未见过(?X-??)。这里发生了什么?

1 个答案:

答案 0 :(得分:4)

?Char给出Char的ASCII码。

?X = "X".ord = 88
?? = "?".ord = 63
?X - ?? = 88-63 = 25
There is your integer: 25
Then "a"*25 = "aaaaaaaaaaaaaaaaaaaaaaaaa"