Forum: JRuby Catenating two Java Strings in Ruby (SOLVED)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2015-12-11 15:50
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
(https://github.com/jruby/jruby/wiki/MailingLists).

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)
This topic is locked and can not be replied to.