Package org.jruby.api

Class Create

java.lang.Object
org.jruby.api.Create

public class Create extends Object
  • Constructor Details

    • Create

      public Create()
  • Method Details

    • newArray

      public static RubyArray<?> newArray(ThreadContext context)
      Create a new array with the default allocation size
      Parameters:
      context - the current thread context
      Returns:
      the new array
    • allocArray

      public static RubyArray<?> allocArray(ThreadContext context, int length)
      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 context
      length - to allocate
      Returns:
      the new array
    • allocArray

      public static RubyArray<?> allocArray(ThreadContext context, long length)
      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 context
      length - to allocate
      Returns:
      the new array
    • newArray

      public static RubyArray<?> newArray(ThreadContext context, IRubyObject one)
      Create a new array with a single element.
      Parameters:
      context - the current thread context
      one - the lone element in the array
      Returns:
      the new array
    • newArray

      public static RubyArray<?> newArray(ThreadContext context, IRubyObject one, IRubyObject two)
      Create a new array with two elements.
      Parameters:
      context - the current thread context
      one - the lone element in the array
      two - the lone element in the array
      Returns:
      the new array
    • newArray

      public static RubyArray<?> newArray(ThreadContext context, IRubyObject... elements)
      Create a new array with many elements.
      Parameters:
      context - the current thread context
      elements - 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 context
      elements - the elements to transform
      func - 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 context
      elt1 - the first element
      elt2 - the second element
      elt3 - the third element
      Returns:
      the new array
    • newArray

      public static RubyArray<?> newArray(ThreadContext context, List<IRubyObject> list)
      Create a new array with many elements from a java.util.List.
      Parameters:
      context - the current thread context
      list - the elements of the array
      Returns:
      a new array
    • newArrayNoCopy

      public static RubyArray<?> newArrayNoCopy(ThreadContext context, IRubyObject... elements)
      Create a new array with many elements but do not copy the incoming array of elements.
      Parameters:
      context - the current thread context
      elements - 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 context
      elements - the elements of the array
      Returns:
      the new array
    • newHash

      public static RubyHash newHash(ThreadContext context)
      Create a new Hash instance.
      Parameters:
      context - the current thread context
      Returns:
      a new hash
    • newEmptyHash

      public static RubyHash newEmptyHash(ThreadContext context)
      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

      public static RubyHash newSmallHash(ThreadContext context)
      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

      public static RubyHash newSmallHash(ThreadContext context, IRubyObject key, IRubyObject value)
      Create a new Hash instance with the given pair, optimized for space.
      Parameters:
      context - the current thread context
      key - the key
      value - the value
      Returns:
      a new hash
    • newEmptyArray

      public static RubyArray<?> newEmptyArray(ThreadContext context)
      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 use newArray(ThreadContext).
      Parameters:
      context - the current thread context
      Returns:
      the new array
    • newString

      public static RubyString newString(ThreadContext context, byte[] bytes)
      Creates a new RubyString from the provided bytes.
      Parameters:
      context - the current thread context
      bytes - the bytes to become a string
      Returns:
      the new RubyString
    • newString

      public static RubyString newString(ThreadContext context, byte[] bytes, int start, int length)
      Creates a new RubyString from the provided bytes.
      Parameters:
      context - the current thread context
      bytes - the bytes to become a string
      start - start index in source bytes
      length - the length from start index in source bytes
      Returns:
      the new RubyString
    • newString

      public static RubyString newString(ThreadContext context, ByteList bytes)
      Creates a new RubyString from the provided bytelist.
      Parameters:
      context - the current thread context
      bytes - 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 context
      bytes - the bytes to become a string
      Returns:
      the new RubyString
    • newSharedString

      public static RubyString newSharedString(ThreadContext context, ByteList bytes)
      Creates a new RubyString from the provided bytelist but use the supplied encoding if possible. This bytelist may be from a shared source.
      Parameters:
      context - the current thread context
      bytes - the bytes to become a string
      Returns:
      the new RubyString
    • newSharedString

      public 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. This bytelist may be from a shared source.
      Parameters:
      context - the current thread context
      bytes - the bytes to become a string
      encoding - to be used (ignoring encoding of bytelist)
      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 context
      string - the contents to become a string
      Returns:
      the new RubyString
    • newString

      public static RubyString newString(ThreadContext context, String string)
      Creates a new RubyString from the provided java String.
      Parameters:
      context - the current thread context
      string - the contents to become a string
      Returns:
      the new RubyString
    • newFrozenString

      public static RubyString newFrozenString(ThreadContext context, String string)
      Creates a new frozen RubyString from the provided java String.
      Parameters:
      context - the current thread context
      string - the contents to become a string
      Returns:
      the new RubyString
    • newEmptyString

      public static RubyString newEmptyString(ThreadContext context)
      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 use newString(ThreadContext, String).
      Parameters:
      context - the current thread context
      Returns:
      the new string
    • newEmptyString

      public 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. It can still grow but the intention is you think it won't grow (or cannot). If you want a default-size array then use newString(ThreadContext, ByteList).
      Parameters:
      context - the current thread context
      Returns:
      the new string
    • dupString

      public static RubyString dupString(ThreadContext context, RubyString string)
      Duplicate the given string and return a String (original subclass of String is not preserved).
      Parameters:
      context - the current thread context
      string - the string to be duplicated
      Returns:
      the new string
    • newRational

      public static RubyRational newRational(ThreadContext context, long num, long den)
      Create a new Rational with the given long values for numerator and denominator.
      Parameters:
      context - the current thread context
      num - the numerator
      den - the denominator
      Returns:
      a new Rational
    • newStruct

      public static RubyStruct newStruct(ThreadContext context, RubyClass structClass, Block block)
      Create a new Struct.
      Parameters:
      context - the current thread context
      block -
      Returns:
    • newStruct

      public static RubyStruct newStruct(ThreadContext context, RubyClass structClass, IRubyObject arg0, Block block)
      Create a new Struct.
      Parameters:
      context - the current thread context
      arg0 - name of class or first member of struct
      block -
      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 context
      arg0 - name of class or first member of struct
      arg1 - a member of struct
      block -
      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 context
      arg0 - name of class or first member of struct
      arg1 - a member of struct
      arg2 - a member of struct
      block -
      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 context
      structClass - expects either a reference to Struct or a subclass of Struct (Passwd for example)
      args - for thr struct
      block -
      Returns: