// @RubyLevelMethod(name="nitems") public RubyValue nitems() { int n = 0; for (int i = 0; i < size(); i++) { if (get(i) != RubyConstant.QNIL) n++; } return ObjectFactory.createFixnum(n); }
// @RubyLevelMethod(name="new", singleton=true) public static RubyValue newArray(RubyValue receiver, RubyArray args, RubyBlock block) { RubyArray a; if (null == args) { a = new RubyArray(); } else if (null == block) { if (args.get(0) instanceof RubyArray) { a = (RubyArray) args.get(0).clone(); } else { RubyFixnum size = (RubyFixnum) args.get(0); RubyValue default_value = args.get(1); a = ObjectFactory.createArray(size.toInt(), default_value); } } else { RubyFixnum size = (RubyFixnum) args.get(0); a = new RubyArray(); for (int i = 0; i < size.toFloat(); i++) { RubyValue return_value = block.invoke(receiver, ObjectFactory.createFixnum(i)); a.add(return_value); } } a.setRubyClass((RubyClass) receiver); return a; }
// @RubyLevelMethod(name="rindex") public RubyValue rindex(RubyValue arg) { for (int i = size() - 1; i >= 0; i--) { if (get(i).equals(arg)) return ObjectFactory.createFixnum(i); } return RubyConstant.QNIL; }
// @RubyLevelMethod(name="hash") public RubyFixnum hash() { return ObjectFactory.createFixnum(hashCode()); }
// @RubyLevelMethod(name="eql?") public RubyValue opEql(RubyValue v) { return ObjectFactory.createBoolean(equals(v, false)); }
// @RubyLevelMethod(name="length") public RubyFixnum length() { return ObjectFactory.createFixnum(this.array_.size()); }
// @RubyLevelMethod(name="empty?") public RubyValue empty_p() { return ObjectFactory.createBoolean(this.array_.isEmpty()); }
// @RubyLevelMethod(name="pack") public RubyValue pack(RubyValue arg) { String format = ((RubyString) arg).toString(); return ObjectFactory.createString(ArrayPacker.pack(this, format)); }