This is a question more out of curiosity than necessity, but I think it
could get me some insights in the interoperability of JRuby and Java:
Say - just for the sake of discussion - that I have created on the JRuby
side two Java strings, like this
a = java.lang.String.new('xxxx')
b = java.lang.String.new('yyyy')
and I want to catenate the strings and store the result as a Java String
too.
This does not work:
c = a + b # NoMethodError: undefined method `+'
This does work, of course, because it converts the strings temporarily
to Ruby string, by implicitly calling to_s on a and b:
c = java.lang.String.new("#[a}#{b}")
Is there a different way to do this? Why isn’t the + operator defined on
Java strings?
===============================================================
UPDATE: Issue solved, thanks to a contribution by Christian Michon on
the JRuby Mailing List
(MailingLists · jruby/jruby Wiki · GitHub).
The ‘+’ operator on the Java side is just syntactic sugar offered by the
Java compiler, and compiled into an invocation of the ‘concat’ method.
Hence, on the Ruby side, we have to write
c = a.concat(b)