Ejemplo n.º 1
0
    @Specialization
    public RubyArray keys(RubyHash hash) {
      final RubyArray array = new RubyArray(getContext().getCoreLibrary().getArrayClass());

      for (Object key : hash.storage.keySet()) {
        array.push(key);
      }

      return array;
    }
Ejemplo n.º 2
0
    @Specialization
    public RubyArray values(RubyHash hash) {
      final RubyArray array = new RubyArray(getContext().getCoreLibrary().getArrayClass());

      for (Object value : hash.storage.values()) {
        array.push(value);
      }

      return array;
    }
Ejemplo n.º 3
0
    @Specialization
    public RubyArray map(VirtualFrame frame, RubyHash hash, RubyProc block) {
      final RubyArray result = new RubyArray(getContext().getCoreLibrary().getArrayClass());

      for (Map.Entry<Object, Object> entry : hash.storage.entrySet()) {
        result.push(yield(frame, block, entry.getKey(), entry.getValue()));
      }

      return result;
    }
Ejemplo n.º 4
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;
    }