-1

I am doing a quine in Ruby that will display its own code in a new file. I'm trying to do like the following to print the variable itself in the new file:

$var = "Some Code%c$var = %c%s%c % [10,34,$var,34,10]%cSome Code" % [10,34,$var,34,10]

The $var variable is empty in the new file, and this is what I want:

Some Text
$var = "Some Code%c$var = %c%s%c % [10,34,$var,34,10]%cSome Code" % [10,34,$var,34,10]
Some Text

When I launch it, I get this in the new file:

Some Code
$var = "" % [10,$var,10]
Some Code

Help please.

1 Answers1

1

You need to escape the percent sign (%%) located within the string and also wrap the $var assignment expression in parentheses so it's available for the following interpolation like so:

($var = "Some Code%c($var = %c%s%c) %% [10,34,$var,34,10]%cSome Code") % [10,34,$var,34,10]

This should result in the following:

>> ($var = "Some Code%c($var = %c%s%c) %% [10,34,$var,34,10]%cSome Code") % [10,34,$var,34,10]
=> "Some Code\n($var = \"Some Code%c($var = %c%s%c) %% [10,34,$var,34,10]%cSome Code\") % [10,34,$var,34,10]\nSome Code"
dsojevic
  • 1,066
  • 10
  • 11
  • Hello ! It tried to do like you said, but this time it displays all the `%c` and `%s`, but not the new lines or the variable itself... I have this in the new file: `Some Code%c($var = %c%s%c) %% [10,34,$var,34,10]%cSome Code` –  Mar 01 '18 at 14:28
  • If you can provide a little more information I might be able to further help... how are you running/excuting this? Eg. what commands are you running, etc. What version of Ruby are you running this in? Is this the full script or is there more to it? – dsojevic Mar 01 '18 at 22:36