Package org.jruby.api
Class Create
java.lang.Object
org.jruby.api.Create
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic RubyArray
<?> allocArray
(ThreadContext context, int length) Create an empty array with a specific allocated size.static RubyArray
<?> allocArray
(ThreadContext context, long length) Create an empty array with a specific allocated size.static RubyString
dupString
(ThreadContext context, RubyString string) Duplicate the given string and return a String (original subclass of String is not preserved).static RubyArray
<?> newArray
(ThreadContext context) Create a new array with the default allocation sizestatic RubyArray
<?> newArray
(ThreadContext context, Collection<? extends IRubyObject> elements) Create a new array with elements from the given Collection.static RubyArray
<?> newArray
(ThreadContext context, List<IRubyObject> list) Create a new array with many elements from a java.util.List.static RubyArray
<?> newArray
(ThreadContext context, IRubyObject one) Create a new array with a single element.static RubyArray
<?> newArray
(ThreadContext context, IRubyObject... elements) Create a new array with many elements.static RubyArray
<?> newArray
(ThreadContext context, IRubyObject one, IRubyObject two) Create a new array with two elements.static RubyArray
<?> newArray
(ThreadContext context, IRubyObject elt1, IRubyObject elt2, IRubyObject elt3) Create a new array with three elements.static <T> RubyArray
<?> newArrayFrom
(ThreadContext context, T[] elements, BiFunction<ThreadContext, T, IRubyObject> func) Create a new Array by applying the given function to the given elements.static RubyArray
<?> newArrayNoCopy
(ThreadContext context, IRubyObject... elements) Create a new array with many elements but do not copy the incoming array of elements.static RubyArray
<?> newEmptyArray
(ThreadContext context) Create a new array which is intended to be empty for its entire lifetime.static RubyHash
newEmptyHash
(ThreadContext context) Create a new empty Hash instance.static RubyString
newEmptyString
(ThreadContext context) Create a new String which is intended to be empty for its entire lifetime.static RubyString
newEmptyString
(ThreadContext context, org.jcodings.Encoding encoding) Create a new String which is intended to be empty for its entire lifetime with a specific encoding.static RubyString
newFrozenString
(ThreadContext context, String string) Creates a new frozen RubyString from the provided java String.static RubyHash
newHash
(ThreadContext context) Create a new Hash instance.static RubyRational
newRational
(ThreadContext context, long num, long den) Create a new Rational with the given long values for numerator and denominator.static RubyString
newSharedString
(ThreadContext context, ByteList bytes) Creates a new RubyString from the provided bytelist but use the supplied encoding if possible.static RubyString
newSharedString
(ThreadContext context, ByteList bytes, org.jcodings.Encoding encoding) Creates a new RubyString from the provided bytelist but use the supplied encoding if possible.static RubyHash
newSmallHash
(ThreadContext context) Create a new Hash instance.static RubyHash
newSmallHash
(ThreadContext context, IRubyObject key, IRubyObject value) Create a new Hash instance with the given pair, optimized for space.static RubyString
newString
(ThreadContext context, byte[] bytes) Creates a new RubyString from the provided bytes.static RubyString
newString
(ThreadContext context, byte[] bytes, int start, int length) Creates a new RubyString from the provided bytes.static RubyString
newString
(ThreadContext context, String string) Creates a new RubyString from the provided java String.static RubyString
newString
(ThreadContext context, String string, org.jcodings.Encoding encoding) Creates a new RubyString from the provided java String.static RubyString
newString
(ThreadContext context, ByteList bytes) Creates a new RubyString from the provided bytelist.static RubyString
newString
(ThreadContext context, ByteList bytes, org.jcodings.Encoding encoding) Creates a new RubyString from the provided bytelist but use the supplied encoding if possible.static RubyStruct
newStruct
(ThreadContext context, RubyClass structClass, Block block) Create a new Struct.static RubyStruct
newStruct
(ThreadContext context, RubyClass structClass, IRubyObject[] args, Block block) Create a new Struct (prefer 0-3 arity versions of this function if you know you arity and it is Struct and not a subclass of Struct).static RubyStruct
newStruct
(ThreadContext context, RubyClass structClass, IRubyObject arg0, Block block) Create a new Struct.static RubyStruct
newStruct
(ThreadContext context, RubyClass structClass, IRubyObject arg0, IRubyObject arg1, Block block) Create a new Struct.static RubyStruct
newStruct
(ThreadContext context, RubyClass structClass, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) Create a new Struct.
-
Constructor Details
-
Create
public Create()
-
-
Method Details
-
newArray
Create a new array with the default allocation size- Parameters:
context
- the current thread context- Returns:
- the new array
-
allocArray
Create an empty array with a specific allocated size. This should be used to make an array where you think you know how big the array will be and you plan on adding data after its construction. It is ok if you add more than this size (it will resize) or less than this size (it will waste a little more space). The goal is to size in a way where we will not have to arraycopy data when the Array grows.- Parameters:
context
- the current thread contextlength
- to allocate- Returns:
- the new array
-
allocArray
Create an empty array with a specific allocated size. This should be used to make an array where you think you know how big the array will be and you plan on adding data after its construction. It is ok if you add more than this size (it will resize) or less than this size (it will waste a little more space). The goal is to size in a way where we will not have to arraycopy data when the Array grows. This version differs from the int override in that it verifies your long value can fit into an int. It will raise if it cannot.- Parameters:
context
- the current thread contextlength
- to allocate- Returns:
- the new array
-
newArray
Create a new array with a single element.- Parameters:
context
- the current thread contextone
- the lone element in the array- Returns:
- the new array
-
newArray
Create a new array with two elements.- Parameters:
context
- the current thread contextone
- the lone element in the arraytwo
- the lone element in the array- Returns:
- the new array
-
newArray
Create a new array with many elements.- Parameters:
context
- the current thread contextelements
- the elements of the array- Returns:
- the new array
-
newArrayFrom
public static <T> RubyArray<?> newArrayFrom(ThreadContext context, T[] elements, BiFunction<ThreadContext, T, IRubyObject> func) Create a new Array by applying the given function to the given elements. The resulting Array will be constructed with a minimum amount of allocation.- Parameters:
context
- the current thread contextelements
- the elements to transformfunc
- the transformation function- Returns:
- the new array
-
newArray
public static RubyArray<?> newArray(ThreadContext context, IRubyObject elt1, IRubyObject elt2, IRubyObject elt3) Create a new array with three elements.- Parameters:
context
- the current thread contextelt1
- the first elementelt2
- the second elementelt3
- the third element- Returns:
- the new array
-
newArray
Create a new array with many elements from a java.util.List.- Parameters:
context
- the current thread contextlist
- the elements of the array- Returns:
- a new array
-
newArrayNoCopy
Create a new array with many elements but do not copy the incoming array of elements.- Parameters:
context
- the current thread contextelements
- the elements of the array- Returns:
- a new array
-
newArray
public static RubyArray<?> newArray(ThreadContext context, Collection<? extends IRubyObject> elements) Create a new array with elements from the given Collection.- Parameters:
context
- the current thread contextelements
- the elements of the array- Returns:
- the new array
-
newHash
Create a new Hash instance.- Parameters:
context
- the current thread context- Returns:
- a new hash
-
newEmptyHash
Create a new empty Hash instance. The expectation is that it will remain empty, so we minimize allocation. TODO: make this actually avoid allocating buckets by fixing RubyHash support for zero buckets.- Parameters:
context
- the current thread context- Returns:
- a new hash
-
newSmallHash
Create a new Hash instance. Use this when you expect very few pairs. TODO: provide a way to allocate a sized small hash- Parameters:
context
- the current thread context- Returns:
- a new hash
-
newSmallHash
Create a new Hash instance with the given pair, optimized for space.- Parameters:
context
- the current thread contextkey
- the keyvalue
- the value- Returns:
- a new hash
-
newEmptyArray
Create a new array which is intended to be empty for its entire lifetime. It can still grow but the intention is you think it won't grow (or cannot). If you want a default-size array then usenewArray(ThreadContext)
.- Parameters:
context
- the current thread context- Returns:
- the new array
-
newString
Creates a new RubyString from the provided bytes.- Parameters:
context
- the current thread contextbytes
- the bytes to become a string- Returns:
- the new RubyString
-
newString
Creates a new RubyString from the provided bytes.- Parameters:
context
- the current thread contextbytes
- the bytes to become a stringstart
- start index in source byteslength
- the length from start index in source bytes- Returns:
- the new RubyString
-
newString
Creates a new RubyString from the provided bytelist.- Parameters:
context
- the current thread contextbytes
- the bytes to become a string- Returns:
- the new RubyString
-
newString
public static RubyString newString(ThreadContext context, ByteList bytes, org.jcodings.Encoding encoding) Creates a new RubyString from the provided bytelist but use the supplied encoding if possible.- Parameters:
context
- the current thread contextbytes
- the bytes to become a string- Returns:
- the new RubyString
-
newString
public static RubyString newString(ThreadContext context, String string, org.jcodings.Encoding encoding) Creates a new RubyString from the provided java String.- Parameters:
context
- the current thread contextstring
- the contents to become a string- Returns:
- the new RubyString
-
newString
Creates a new RubyString from the provided java String.- Parameters:
context
- the current thread contextstring
- the contents to become a string- Returns:
- the new RubyString
-
newFrozenString
Creates a new frozen RubyString from the provided java String.- Parameters:
context
- the current thread contextstring
- the contents to become a string- Returns:
- the new RubyString
-
newEmptyString
Create a new String which is intended to be empty for its entire lifetime. It can still grow but the intention is you think it won't grow (or cannot). If you want a default-size array then usenewString(ThreadContext, String)
.- Parameters:
context
- the current thread context- Returns:
- the new string
-
newEmptyString
Create a new String which is intended to be empty for its entire lifetime with a specific encoding. It can still grow but the intention is you think it won't grow (or cannot). If you want a default-size array then usenewString(ThreadContext, ByteList)
.- Parameters:
context
- the current thread context- Returns:
- the new string
-
dupString
Duplicate the given string and return a String (original subclass of String is not preserved).- Parameters:
context
- the current thread contextstring
- the string to be duplicated- Returns:
- the new string
-
newRational
Create a new Rational with the given long values for numerator and denominator.- Parameters:
context
- the current thread contextnum
- the numeratorden
- the denominator- Returns:
- a new Rational
-
newStruct
Create a new Struct.- Parameters:
context
- the current thread contextblock
-- Returns:
-
newStruct
public static RubyStruct newStruct(ThreadContext context, RubyClass structClass, IRubyObject arg0, Block block) Create a new Struct.- Parameters:
context
- the current thread contextarg0
- name of class or first member of structblock
-- Returns:
-
newStruct
public static RubyStruct newStruct(ThreadContext context, RubyClass structClass, IRubyObject arg0, IRubyObject arg1, Block block) Create a new Struct.- Parameters:
context
- the current thread contextarg0
- name of class or first member of structarg1
- a member of structblock
-- Returns:
-
newStruct
public static RubyStruct newStruct(ThreadContext context, RubyClass structClass, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) Create a new Struct.- Parameters:
context
- the current thread contextarg0
- name of class or first member of structarg1
- a member of structarg2
- a member of structblock
-- Returns:
-
newStruct
public static RubyStruct newStruct(ThreadContext context, RubyClass structClass, IRubyObject[] args, Block block) Create a new Struct (prefer 0-3 arity versions of this function if you know you arity and it is Struct and not a subclass of Struct).- Parameters:
context
- the current thread contextstructClass
- expects either a reference to Struct or a subclass of Struct (Passwd for example)args
- for thr structblock
-- Returns:
-