Catenating two Java Strings in Ruby (SOLVED)

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 ='xxxx')
b ='yyyy')

and I want to catenate the strings and store the result as a Java String

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 ="#[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

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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs