pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/jruby/jruby/commit/fe30f82f7f9fe5df59043fcf2be97cc773408bb1

r-b48faa60c69660fa.css" /> Merge pull request #7238 from kares/map-key-check · jruby/jruby@fe30f82 · GitHub
Skip to content

Commit fe30f82

Browse files
authored
Merge pull request #7238 from kares/map-key-check
[ji] proper key? checking semantics for Map
2 parents 6ab4d68 + cc4afb3 commit fe30f82

4 files changed

Lines changed: 42 additions & 8 deletions

File tree

core/src/main/java/org/jruby/RubyDir.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ public static Optional<String> getHomeFromEnv(Ruby runtime) {
11061106
final RubyString homeKey = RubyString.newStringShared(runtime, HOME);
11071107
final RubyHash env = runtime.getENV();
11081108

1109-
if (env.has_key_p(homeKey).isFalse()) {
1109+
if (!env.hasKey(homeKey)) {
11101110
return Optional.empty();
11111111
} else {
11121112
return Optional.of(env.op_aref(runtime.getCurrentContext(), homeKey).toString());

core/src/main/java/org/jruby/RubyHash.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,12 +1384,21 @@ public IRubyObject fetch(ThreadContext context, IRubyObject key, IRubyObject _de
13841384
*/
13851385
@JRubyMethod(name = {"has_key?", "key?", "include?", "member?"}, required = 1)
13861386
public RubyBoolean has_key_p(ThreadContext context, IRubyObject key) {
1387-
return internalGetEntry(key) == NO_ENTRY ? context.fals : context.tru;
1387+
return hasKey(key) ? context.tru : context.fals;
13881388
}
13891389

1390+
@Deprecated
13901391
public RubyBoolean has_key_p(IRubyObject key) {
1391-
Ruby runtime = metaClass.runtime;
1392-
return internalGetEntry(key) == NO_ENTRY ? runtime.getFalse() : runtime.getTrue();
1392+
return has_key_p(metaClass.runtime.getCurrentContext(), key);
1393+
}
1394+
1395+
/**
1396+
* A Java API to test the presence of a (Ruby) key in the Hash
1397+
* @param key the native (Ruby) key
1398+
* @return true if the hash contains the provided key
1399+
*/
1400+
public boolean hasKey(IRubyObject key) {
1401+
return internalGetEntry(key) != NO_ENTRY;
13931402
}
13941403

13951404
private static class Found extends RuntimeException {

core/src/main/java/org/jruby/java/proxies/MapJavaProxy.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,24 @@ public <T> void visitAll(ThreadContext context, VisitorWithState visitor, T stat
259259
}
260260

261261
@Override
262+
public boolean hasKey(IRubyObject key) {
263+
return mapDelegate().containsKey(key);
264+
}
265+
266+
@Override
267+
public RubyBoolean has_key_p(ThreadContext context, IRubyObject key) {
268+
return mapDelegate().containsKey(key.toJava(Object.class)) ? context.tru : context.fals;
269+
}
270+
271+
@Override
272+
@Deprecated
262273
public RubyBoolean has_key_p(IRubyObject key) {
263-
final Object convertedKey = key.toJava(Object.class);
264-
return getRuntime().newBoolean( mapDelegate().containsKey(convertedKey) );
274+
return has_key_p(getRuntime().getCurrentContext(), key);
265275
}
266276

267277
@Override
268278
public RubyBoolean has_value_p(ThreadContext context, IRubyObject val) {
269-
final Object convertedVal = val.toJava(Object.class);
270-
return getRuntime().newBoolean( mapDelegate().containsValue(convertedVal) );
279+
return mapDelegate().containsValue(val.toJava(Object.class)) ? context.tru : context.fals;
271280
}
272281

273282
@Override

spec/java_integration/types/map_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@
7777
expect( m.empty? ).to be true
7878
end
7979

80+
it "properly handles key? when key included in the map" do
81+
map = java.util.HashMap.new
82+
map[:foo] = 'bar'
83+
map[:baz] = nil
84+
expect( map.key?(:foo) ).to be true
85+
expect( map.key?(:baz) ).to be true
86+
expect( map.key?(:bar) ).to be false
87+
expect( map.key?('bar') ).to be false
88+
89+
map = java.util.LinkedHashMap.new 'foo' => nil
90+
expect( map.include?('foo') ).to be true
91+
expect( map.has_key?('foo') ).to be true
92+
expect( map.member?('foo') ).to be true
93+
expect( map.include?('bar') ).to be false
94+
end
95+
8096
it "supports Hash-like operations" do
8197
h = java.util.HashMap.new
8298
test_ok(h.kind_of? java.util.Map)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy