0

If I want to add a control character to a string, is there a method to do so? I tried looking at the regexp class in the API, but that only seems to be relevant when you are searching for a control character.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294

1 Answers1

3

You can use format like \cx in double quoted string to represent control character x.

For instance:

"\cA\cD\cH"
#=> "\u0001\u0004\b"

For single character strings, this could also work:

?\C-A
#=> "\u0001"
?\C-H
#=> "\b"
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Thank you, this is great. If I use ctrlD = "\cD" and then place the variable in a string like so `#{ctrlD}`, will it succesfully place the EOT control character in the string? –  Oct 31 '14 at 14:34