1 32 package org.jruby.ast.util; 33 34 import org.jruby.Ruby; 35 import org.jruby.RubyArray; 36 import org.jruby.runtime.builtin.IRubyObject; 37 38 42 public final class ArgsUtil { 43 44 public static IRubyObject[] convertToJavaArray(IRubyObject value) { 45 if (value == null) { 46 return IRubyObject.NULL_ARRAY; 47 } 48 49 if (value instanceof RubyArray) { 50 return ((RubyArray)value).toJavaArrayMaybeUnsafe(); 51 } 52 53 return new IRubyObject[] { value }; 54 } 55 56 64 public static RubyArray convertToRubyArray(Ruby runtime, IRubyObject value, boolean coerce) { 65 if (value == null) { 66 return runtime.newArray(0); 67 } 68 69 if (!coerce) { 70 return runtime.newArray(value); 72 } 73 74 IRubyObject newValue = value.convertToType("Array", "to_ary", false); 76 77 if (newValue.isNil()) { 78 return runtime.newArray(value); 79 } 80 81 return (RubyArray)newValue; 84 } 85 86 92 public static IRubyObject[] popArray(IRubyObject[] array) { 93 if (array == null || array.length == 0) { 94 return IRubyObject.NULL_ARRAY; 95 } 96 97 IRubyObject[] newArray = new IRubyObject[array.length - 1]; 98 System.arraycopy(array, 1, newArray, 0, array.length - 1); 99 100 return newArray; 101 } 102 } 103 | Popular Tags |