Beispiel #1
0
    @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;
      }
    }
Beispiel #2
0
    @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;
      }
    }
Beispiel #3
0
    @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;
    }
Beispiel #4
0
 @Specialization
 public Object construct(RubyHash hash, Object index, Object value) {
   hash.put(index, value);
   return value;
 }
Beispiel #5
0
 @Specialization
 public NilPlaceholder initialize(RubyHash hash, RubyProc block) {
   hash.initialize(block);
   return NilPlaceholder.INSTANCE;
 }
Beispiel #6
0
 @Specialization
 public NilPlaceholder initialize(
     RubyHash hash, @SuppressWarnings("unused") UndefinedPlaceholder block) {
   hash.initialize(null);
   return NilPlaceholder.INSTANCE;
 }