Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling RubyHash in java code #4488

Closed
hydrogen18 opened this issue Feb 13, 2017 · 1 comment
Closed

Handling RubyHash in java code #4488

hydrogen18 opened this issue Feb 13, 2017 · 1 comment
Milestone

Comments

@hydrogen18
Copy link

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?

@headius
Copy link
Member

headius commented Feb 13, 2017

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.

Hopefully this answers your questions!

@headius headius closed this as completed Feb 13, 2017
@headius headius added this to the Non-Release milestone Feb 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants