@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; }
@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; }
@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; }
@Specialization public RubyArray unpack(RubyString string, RubyString format) { final org.jruby.RubyArray jrubyArray = Pack.unpack(getContext().getRuntime(), string.getBytes(), format.getBytes()); return RubyArray.specializedFromObjects( getContext().getCoreLibrary().getArrayClass(), jrubyArray.toArray()); }
@Specialization public RubyArray split(RubyString string, RubyString sep) { final String[] components = string.toString().split(Pattern.quote(sep.toString())); final Object[] objects = new Object[components.length]; for (int n = 0; n < objects.length; n++) { objects[n] = getContext().makeString(components[n]); } return RubyArray.specializedFromObjects( getContext().getCoreLibrary().getArrayClass(), objects); }
@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 RubyArray split(RubyString string, RubyRegexp sep) { return RubyArray.specializedFromObjects( getContext().getCoreLibrary().getArrayClass(), sep.split(string.toString())); }
@Specialization public RubyArray scan(RubyString string, RubyRegexp regexp) { return RubyArray.specializedFromObjects( getContext().getCoreLibrary().getArrayClass(), regexp.scan(string)); }