One solution is Ruby's implementation of the heredoc. Essentially, you're saying, "This is a string. Go ahead and process this as a string until I give you the magic word. In the following example, "DOCUMENT" is the magic word.
>> secret = 'PASSWORD'
=> "PASSWORD"
>> my_text = <<-DOCUMENT Yoda says, 'confusing to escape this is: """ """" ;;;; ''''' .' My password is secretly #{secret} you could go on for a while here. DOCUMENT
=> " Yoda says, 'confusing to escape this is: \"\"\" \"\"\"\" ;;;; ''''' .'\n My password is secretly PASSWORD\n you could go on for a while here.\n"So that's nice. Assuming you don't have #{} anywhere in your text, this works great. However, the word DOCUMENT doesn't exactly jump out as obvious.
Andy Stone showed a syntax he often uses. Though the syntax is perl-like in it cryptic-ness, if that is a word, and I think that it is not, still, it seems better. Closing token is a pipe ( ), which Andy says he doesn't often find in strings.
>> my_text = %
Yoda says, 'confusing to escape this is: """ """" ;;;; ''''' .'
My password is secretly #{secret}
you could go on for a while here.
=> "\n Yoda says, 'confusing to escape this is: \"\"\" \"\"\"\" ;;;; ''''' .'\n My password is secretly PASSWORD\n you could go on for a while here.\n"
It's not perfect, but certainly better. Certainly the pipe is a nicer closing token.
0 comments:
Post a Comment