Summary of the problem
I'd like to try Ruby for something I did in Python. In Python it has the r""" syntax to support raw strings, which is nice as it allows one to have raw strings in-line with the code and to concatenate them in more natural way and with no special indentation needed. In Ruby, when using raw strings, one has to use <<'EOT' followed by EOT in separate line which breaks the code layout.
You might ask, why not then use Ruby's %q{}? Well, because %q{} has limitations compared to Python's r""" as it does not escape multiple \\\ and only handles single \.
I am generating Latex code on the fly and write to a file, which later is compiled with pdflatex. The Latex code contain things like \\\ in many places. If I use Ruby's %q{} syntax, then it will not work. So I have to use Ruby's <<'EOT' but I do not want to do this, as it makes the code harder to read in the Ruby source file due to having to break it for indentation to make EOT happy.
I am asking if there is a way to make syntax similar to %q{}, or some function that take string and return same result as if one used EOT, that handles raw strings without the limitation of EOT.
I do not need interpolation. So single quoted strings only. No double quoted. double quotes causes interpolation, which I do not want.
Small working examples to illustrate
Here is a small example in Python, and then I show what I have to do in Ruby to generate the same output.
my_file = open("py_latex.tex", 'w')
x = r"""\\\hline is a raw string"""+r""" another one \\\hline and so on"""
my_file.write(x)
When I open the Latex text file written to in the above, I see the correct result
Now to do the same thing in Ruby. I can't write the following (even though I'd like to)
file = File.open('rb_latex.tex','w')
x=%q{\\\hline is a raw string}+%q{ another one \\\hline and so on}
file.write(x)
The above ofcourse is not what I want. When it is written to latex file, it shows up as
Using EOT works, as follows
file = File.open('rb_latex.tex','w')
x=<<-'EOT1'+<<-'EOT2'
\\\hline is a raw string
EOT1
another one \\\hline and so on
EOT2
file.write(x)
And the file now is
ps. it makes the second string on new line, this is a side-issue for me now, and will try to find solution for this after I solve the main problem at hand.
Short summary of the question
How to make %q{} like syntax for Ruby that works similar to Python r""" ?
If someone wants to try the above code in Ruby, make sure there is no space after EOT. I also include below the full source code.
Python full source
import os
os.chdir(" direct to change to here ")
my_file = open("py_latex.tex", 'w')
x = r"""\\\hline is a raw string"""+r""" another one \\\hline and so on"""
my_file.write(x)
my_file.close()
Ruby source code
#!/usr/local/bin/ruby -w
Dir.chdir("/home/....")
file = File.open('rb_latex.tex','w')
#x=%q{\\\hline is a raw string}+%q{ another one \\\hline and so on}
x=<<-'EOT1'+<<-'EOT2'
\\\hline is a raw string
EOT1
another one \\\hline and so on
EOT2
file.write(x)
file.close
Aucun commentaire:
Enregistrer un commentaire