You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Should a method that is an @JRubyMethod treat a hash as a java.util.Map or a RubyHash
If I use java.util.Map and the entrySet() method for iteration, when I call getValue() and getKey() I can't tell what I should expect. Sometimes I get java strings, sometimes I get RubyString, etc.
If I use RubyHash and the directEntrySet() I always get RubyObject from getValue() and getKey() which I can safely cast using my own code or JavaUtil static methods.
Is there any reason to try and treat a RubyHash as a java.util.Map or does it confer no advantage?
The text was updated successfully, but these errors were encountered:
JRubyMethod methods implemented in Java for Ruby behavior should generally treat a hash as RubyHash.
For right or for wrong, when we opted to implement the java.util.collection.Map and related interfaces, we made them coerce several types of Ruby values (numbers, strings) to their Java equivalents rather than passing e.g. RubyFixnum or RubyString directly out. These interfaces are intended to be used by Java code that has no awareness of Ruby, and the automatic coercion is to aid integration with such code.
If you like, this might be a good thing to add to the wiki docs about Java integration or the docs about implementing JRuby extensions in Java.
Should a method that is an
@JRubyMethod
treat a hash as ajava.util.Map
or aRubyHash
If I use
java.util.Map
and theentrySet()
method for iteration, when I callgetValue()
andgetKey()
I can't tell what I should expect. Sometimes I get java strings, sometimes I getRubyString
, etc.If I use
RubyHash
and thedirectEntrySet()
I always getRubyObject
fromgetValue()
andgetKey()
which I can safely cast using my own code orJavaUtil
static methods.Is there any reason to try and treat a
RubyHash
as ajava.util.Map
or does it confer no advantage?The text was updated successfully, but these errors were encountered: