String 我可以用什么字符来引用这个ruby字符串?

String 我可以用什么字符来引用这个ruby字符串?,string,jruby,embedded-language,String,Jruby,Embedded Language,我正在Java中嵌入JRuby,因为我需要用Java字符串作为参数调用一些Ruby方法。问题是,我调用的方法如下: String text = ""; // this can span over multiple lines, and will contain ruby code Ruby ruby = Ruby.newInstance(); RubyRuntimeAdapter adapter = JavaEmbedUtils.newRuntimeAdapter(); String ruby

我正在Java中嵌入JRuby,因为我需要用Java字符串作为参数调用一些Ruby方法。问题是,我调用的方法如下:


String text = ""; // this can span over multiple lines, and will contain ruby code
Ruby ruby = Ruby.newInstance();
RubyRuntimeAdapter adapter = JavaEmbedUtils.newRuntimeAdapter();
String rubyCode = "require \"myscript\"\n" +
                            "str = build_string(%q~"+text+"~)\n"+
                            "str";
IRubyObject object = adapter.eval(ruby, codeFormat);

问题是,我不知道我可以使用什么字符串作为分隔符,因为如果我发送给build_string的ruby代码将包含ruby代码。是的,我知道我正在使用~,但我认为这可能会破坏我的代码。无论ruby代码是什么,我都可以使用哪些字符作为分隔符来确保我的代码能够正常工作?

由于字符串文本包含任何字符,因此没有任何字符可以像您现在使用的~那样用于引号转义。在java中,您仍然需要在字符串文本中转义tilde,并将其附加到正在构建的字符串中

类似(未经测试,不是Java大师):


使用heredoc格式:

"require \"myscript\"\n" +
          "str = build_string(<<'THISSHOUDLNTBE'\n" + text + "\nTHISSHOULDNTBE\n)\n"+
          "str";
“需要\“myscript\”\n+

str=build_string(我将生成一个以herdoc开头和结尾的字符串。
"require \"myscript\"\n" +
          "str = build_string(<<'THISSHOUDLNTBE'\n" + text + "\nTHISSHOULDNTBE\n)\n"+
          "str";