Harsh Gupta
Harsh Gupta

Reputation: 4538

Ruby string repetition without integer

I just found a CodeGolf answer here http://repl.it/2Om/6:

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-??)

Original post: https://codegolf.stackexchange.com/a/40250

I am curious, how does it work? I have never seen (?X-??) before. What's happening here?

Upvotes: 0

Views: 91

Answers (1)

kyflare
kyflare

Reputation: 874

?Char gives the ASCII code of Char.

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

Upvotes: 4

Related Questions