@JRubyMethod public IRubyObject keys(ThreadContext context) { return RubyArray.newArray(context.runtime, table.keySet()); }
for (int i = 0; i < arr.size(); i++) { this.storage.add(arr.eltInternal(i));
@JRubyMethod public IRubyObject concat(ThreadContext context, IRubyObject list) { if (list instanceof RubyArray) { checkArrayElementType(context, (RubyArray) list); this.storage.addAll((RubyArray) list); } else { RubyRepeatedField repeatedField = (RubyRepeatedField) list; if (! fieldType.equals(repeatedField.fieldType) || (typeClass != null && ! typeClass.equals(repeatedField.typeClass))) throw context.runtime.newArgumentError("Attempt to append RepeatedField with different element type."); this.storage.addAll((RubyArray) repeatedField.toArray(context)); } return this.storage; }
private void checkArrayElementType(ThreadContext context, RubyArray arr) { for (int i = 0; i < arr.getLength(); i++) { Utils.checkType(context, fieldType, arr.eltInternal(i), (RubyModule) typeClass); } }
protected RubyRepeatedField deepCopy(ThreadContext context) { RubyRepeatedField copy = new RubyRepeatedField(context.runtime, metaClass, fieldType, typeClass); for (int i = 0; i < size(); i++) { IRubyObject value = storage.eltInternal(i); if (fieldType == Descriptors.FieldDescriptor.Type.MESSAGE) { copy.storage.add(((RubyMessage) value).deepCopy(context)); } else { copy.storage.add(value); } } return copy; }
@JRubyMethod public IRubyObject dup(ThreadContext context) { RubyRepeatedField dup = new RubyRepeatedField(context.runtime, metaClass, fieldType, typeClass); for (int i = 0; i < this.storage.size(); i++) { dup.push(context, this.storage.eltInternal(i)); } return dup; }
@JRubyMethod(name = "finalize_to_pool") public IRubyObject finalizeToPool(ThreadContext context, IRubyObject rbPool) { RubyDescriptorPool pool = (RubyDescriptorPool) rbPool; for (int i = 0; i < this.pendingList.size(); i++) { IRubyObject defRb = this.pendingList.entry(i); if (defRb instanceof RubyDescriptor) { pool.addToSymtab(context, (RubyDescriptor) defRb); } else { pool.addToSymtab(context, (RubyEnumDescriptor) defRb); } } this.pendingList = context.runtime.newArray(); return context.runtime.getNil(); }
@Override @SuppressWarnings("unchecked") public Object getJavaObject() { if ( irubyObject == null || ! fromRuby ) return javaObject; final RubyArray ary = (RubyArray) irubyObject; if (javaType == null) { // firstly retrieved from Ruby return javaObject = new ArrayList<String>(ary); } else if (javaType == String[].class) { String[] strArr = new String[ ary.size() ]; for ( int i=0; i<ary.size(); i++ ) { strArr[i] = (String) ary.get(i); } return javaObject = strArr; } else if (javaObject instanceof List) { try { ((List) javaObject).clear(); ((List) javaObject).addAll(ary); } catch (UnsupportedOperationException e) { // no op. no way to update. } return javaObject; } return null; }
private IRubyObject yieldInner(ThreadContext context, RubyArray array, Block blockArg) { if (array.isEmpty()) { throw context.runtime.newArgumentError("no receiver given"); } final IRubyObject self = array.shift(context); return site.call(context, self, self, array.toJavaArray(), blockArg); }
@Deprecated // not used public static IRubyObject arrayPostOrNilTwo(RubyArray array, int pre, int post) { if (pre + post < array.getLength()) { return array.eltInternal(array.getLength() - post + 2); } else if (pre + 2 < array.getLength()) { return array.eltInternal(pre + 2); } else { return array.getRuntime().getNil(); } }
@JRubyMethod(name = "any?", optional = 1) public IRubyObject any_p(ThreadContext context, IRubyObject[] args, Block block) { if (isEmpty()) return context.fals; if (!isBuiltin("each")) return RubyEnumerable.any_pCommon(context, this, args, block); boolean patternGiven = args.length > 0; if (!block.isGiven() || patternGiven) return any_pBlockless(context, args); for (int i = 0; i < realLength; i++) { if (block.yield(context, eltOk(i)).isTrue()) return context.tru; } return context.fals; }
tmp = ((RubyArray)tmp).aryDup(); eargp = execargNew(context, ((RubyArray)tmp).toJavaArray(), false); ((RubyArray)tmp).clear(); } else { pname = pname.convertToString();
@JRubyMethod(required=1, optional=1, name = {"at", "[]"}) public IRubyObject index(ThreadContext context, IRubyObject[] args) { if (args.length == 1){ IRubyObject arg = args[0]; if (Utils.isRubyNum(arg)) { /* standard case */ int arrIndex = normalizeArrayIndex(arg); if (arrIndex < 0 || arrIndex >= this.storage.size()) { return context.runtime.getNil(); } return this.storage.eltInternal(arrIndex); } else if (arg instanceof RubyRange) { RubyRange range = ((RubyRange) arg); int beg = RubyNumeric.num2int(range.first(context)); int to = RubyNumeric.num2int(range.last(context)); int len = to - beg + 1; return this.storage.subseq(beg, len); } } /* assume 2 arguments */ int beg = RubyNumeric.num2int(args[0]); int len = RubyNumeric.num2int(args[1]); if (beg < 0) { beg += this.storage.size(); } if (beg >= this.storage.size()) { return context.runtime.getNil(); } return this.storage.subseq(beg, len); }
@JRubyMethod(name = {"to_h", "to_hash"}) public IRubyObject toHash(ThreadContext context) { Ruby runtime = context.runtime; RubyHash ret = RubyHash.newHash(runtime); for (Descriptors.FieldDescriptor fdef : this.descriptor.getFields()) { IRubyObject value = getField(context, fdef); if (!value.isNil()) { if (fdef.isRepeated() && !fdef.isMapField()) { if (fdef.getType() != Descriptors.FieldDescriptor.Type.MESSAGE) { value = Helpers.invoke(context, value, "to_a"); } else { RubyArray ary = value.convertToArray(); for (int i = 0; i < ary.size(); i++) { IRubyObject submsg = Helpers.invoke(context, ary.eltInternal(i), "to_h"); ary.eltInternalSet(i, submsg); } value = ary.to_ary(); } } else if (value.respondsTo("to_h")) { value = Helpers.invoke(context, value, "to_h"); } else if (value.respondsTo("to_a")) { value = Helpers.invoke(context, value, "to_a"); } } ret.fastASet(runtime.newSymbol(fdef.getName()), value); } return ret; }
@JRubyMethod(name = "<<", rest = true) public IRubyObject op_lshift(ThreadContext context, IRubyObject[]args) { if (args.length == 1 && args[0] instanceof RubyArray && ((RubyArray) args[0]).getLength() == 1) args[0] = RubyArray.newArray(context.runtime, args[0]); yield(context, args); return this; } }