Search code examples
golfscript

ways to read a text file in golfscript and print out its content


'#{File.read("file")}' puts

Does not work. Is it possible to read in the content of a text file in GolfScript?


Solution

  • The #{...} expansion only occurs with double-quoted strings:

    "#{File.read('file')}" puts
    

    works fine.

    However, there are some catches.

    1. If you want 'file' to be a parameter, you have to delay the expansion.
    2. The result is cached the first time, so if you want to read the same file more than once (e.g. to check for changes) you have to ensure that the expanded value changes. The easiest way I know to do this is to expand "#{File.read('file')#1}", "#{File.read('file')#2}", etc.