@Specialization public Object delete(RubyHash hash, Object index) { hash.checkFrozen(); final Object value = hash.getMap().remove(index); if (value == null) { return NilPlaceholder.INSTANCE; } else { return value; } }
@Specialization public Object construct(VirtualFrame frame, RubyHash hash, Object index) { final Object value = hash.get(index); if (value == null) { if (hash.defaultBlock == null) { return NilPlaceholder.INSTANCE; } else { return hash.defaultBlock.call(frame.pack(), hash, index); } } else { return value; } }
@Specialization public RubyHash construct(Object[] args) { final RubyHash hash = new RubyHash(getContext().getCoreLibrary().getHashClass()); if (args.length == 1) { final RubyArray array = (RubyArray) args[0]; for (int n = 0; n < array.size(); n++) { final RubyArray keyValue = (RubyArray) array.get(n); hash.put(keyValue.get(0), keyValue.get(1)); } } else { if (args.length % 2 != 0) { // TODO(CS): figure out what error to throw here throw new UnsupportedOperationException(); } for (int n = 0; n < args.length; n += 2) { hash.put(args[n], args[n + 1]); } } return hash; }
@Specialization public Object construct(RubyHash hash, Object index, Object value) { hash.put(index, value); return value; }
@Specialization public NilPlaceholder initialize(RubyHash hash, RubyProc block) { hash.initialize(block); return NilPlaceholder.INSTANCE; }
@Specialization public NilPlaceholder initialize( RubyHash hash, @SuppressWarnings("unused") UndefinedPlaceholder block) { hash.initialize(null); return NilPlaceholder.INSTANCE; }