protected RubyMap mergeIntoSelf(final ThreadContext context, IRubyObject hashmap) { if (hashmap instanceof RubyHash) { ((RubyHash) hashmap).visitAll(new RubyHash.Visitor() { @Override public void visit(IRubyObject key, IRubyObject val) { indexSet(context, key, val); } }); } else if (hashmap instanceof RubyMap) { RubyMap other = (RubyMap) hashmap; if (!typeCompatible(other)) { throw context.runtime.newTypeError("Attempt to merge Map with mismatching types"); } } else { throw context.runtime.newTypeError("Unknown type merging into Map"); } return this; }
hash.visitAll(new RubyHash.Visitor() { @Override public void visit(IRubyObject key, IRubyObject value) {
private boolean hasValue(ThreadContext context, IRubyObject expected) { try { visitAll(context, FoundIfEqualVisitor, expected); return false; } catch (Found found) { return true; } }
private boolean hasValue(ThreadContext context, IRubyObject expected) { try { visitAll(context, FoundIfEqualVisitor, expected); return false; } catch (Found found) { return true; } }
private IRubyObject internalIndex(final ThreadContext context, final IRubyObject expected) { try { visitAll(context, FoundKeyIfEqual, expected); return null; } catch (FoundKey found) { return found.key; } }
public static void checkForExtraUnwantedKeywordArgs(ThreadContext context, final StaticScope scope, RubyHash keywordArgs) { // we do an inexpensive non-gathering scan first to see if there's a bad keyword try { keywordArgs.visitAll(context, CheckUnwantedKeywordsVisitor, scope); } catch (InvalidKeyException ike) { // there's a bad keyword; perform more expensive scan to gather all bad names GatherUnwantedKeywordsVisitor visitor = new GatherUnwantedKeywordsVisitor(); keywordArgs.visitAll(context, visitor, scope); visitor.raiseIfError(context); } }
public static void checkForExtraUnwantedKeywordArgs(ThreadContext context, final StaticScope scope, RubyHash keywordArgs) { // we do an inexpensive non-gathering scan first to see if there's a bad keyword try { keywordArgs.visitAll(context, CheckUnwantedKeywordsVisitor, scope); } catch (InvalidKeyException ike) { // there's a bad keyword; perform more expensive scan to gather all bad names GatherUnwantedKeywordsVisitor visitor = new GatherUnwantedKeywordsVisitor(); keywordArgs.visitAll(context, visitor, scope); visitor.raiseIfError(context); } }
@JRubyMethod(name = "assoc") public IRubyObject assoc(final ThreadContext context, final IRubyObject obj) { try { visitAll(context, FoundPairIfEqualKeyVisitor, obj); return context.nil; } catch (FoundPair found) { return context.runtime.newArray(found.key, found.value); } }
@JRubyMethod(name = "rassoc") public IRubyObject rassoc(final ThreadContext context, final IRubyObject obj) { try { visitAll(context, FoundPairIfEqualValueVisitor, obj); return context.nil; } catch (FoundPair found) { return context.runtime.newArray(found.key, found.value); } }
private <T> void iteratorVisitAll(ThreadContext context, VisitorWithState<T> visitor, T state) { try { iteratorEntry(); visitAll(context, visitor, state); } finally { iteratorExit(); } }
@JRubyMethod(name = "rassoc") public IRubyObject rassoc(final ThreadContext context, final IRubyObject obj) { try { visitAll(context, FoundPairIfEqualValueVisitor, obj); return context.nil; } catch (FoundPair found) { return context.runtime.newArray(found.key, found.value); } }
@JRubyMethod(name = "assoc") public IRubyObject assoc(final ThreadContext context, final IRubyObject obj) { try { visitAll(context, FoundPairIfEqualKeyVisitor, obj); return context.nil; } catch (FoundPair found) { return context.runtime.newArray(found.key, found.value); } }
/** rb_hash_invert * */ @JRubyMethod(name = "invert") public RubyHash invert(final ThreadContext context) { final RubyHash result = newHash(getRuntime()); visitAll(context, InvertVisitor, result); return result; }
/** inspect_hash * */ private IRubyObject inspectHash(final ThreadContext context) { final RubyString str = RubyString.newStringLight(context.runtime, DEFAULT_INSPECT_STR_SIZE, USASCIIEncoding.INSTANCE); str.infectBy(this); str.cat((byte)'{'); visitAll(context, InspectVisitor, str); str.cat((byte)'}'); return str; }
@JRubyMethod(name = "transform_keys") public IRubyObject transform_keys(final ThreadContext context, final Block block) { if (block.isGiven()) { RubyHash result = newHash(context.runtime); visitAll(context, new TransformKeysVisitor(block), result); return result; } return enumeratorizeWithSize(context, this, "transform_keys", enumSizeFn()); }
/** rb_hash_update * */ @JRubyMethod(name = {"merge!", "update"}, required = 1) public RubyHash merge_bang(ThreadContext context, IRubyObject other, Block block) { modify(); final RubyHash otherHash = other.convertToHash(); if (otherHash.empty_p().isTrue()) return this; otherHash.visitAll(context, new MergeVisitor(this), block); return this; }
@JRubyMethod(name = "uniq") public IRubyObject uniq19(ThreadContext context, Block block) { if (!block.isGiven()) return uniq(context); RubyHash hash = makeHash(context, block); RubyArray result = new RubyArray(context.runtime, getMetaClass(), hash.size()); hash.visitAll(context, RubyHash.StoreValueVisitor, result); return result; }
@JRubyMethod(name = "uniq") public IRubyObject uniq19(ThreadContext context, Block block) { if (!block.isGiven()) return uniq(context); RubyHash hash = makeHash(context, block); RubyArray result = new RubyArray(context.runtime, getMetaClass(), hash.size()); hash.visitAll(context, RubyHash.StoreValueVisitor, result); return result; }
@JIT @Interp public static IRubyObject mergeKeywordArguments(ThreadContext context, IRubyObject restKwarg, IRubyObject explicitKwarg) { RubyHash hash = (RubyHash) TypeConverter.checkHashType(context.runtime, restKwarg).dup(); hash.modify(); final RubyHash otherHash = explicitKwarg.convertToHash(); if (otherHash.empty_p().isTrue()) return hash; otherHash.visitAll(context, new KwargMergeVisitor(hash), Block.NULL_BLOCK); return hash; }
@JIT @Interp public static IRubyObject mergeKeywordArguments(ThreadContext context, IRubyObject restKwarg, IRubyObject explicitKwarg) { RubyHash hash = (RubyHash) TypeConverter.checkHashType(context.runtime, restKwarg).dup(); hash.modify(); final RubyHash otherHash = explicitKwarg.convertToHash(); if (otherHash.empty_p().isTrue()) return hash; otherHash.visitAll(context, new KwargMergeVisitor(hash), Block.NULL_BLOCK); return hash; }