RbObjectCollection

A view onto a Ruby array using Swift collection protocols.

This is an adapter type that wraps an RbObject and adopts Swift collection protocols for use with an underlying Ruby array (or any Ruby object that supports length, [], and []=.)

For example:

myObj.collection.replaceSubrange(lower..<upper, with: otherArray)

This is separate to RbObject to avoid dumping all the collection protocol members into its dynamic member lookup namespace.


Topics

init(RbObject)

Create a collection from an existing Ruby array object.

The same thing as accessing RbObject.collection.

Declaration
Swift
public init(_ value: RbObject)

var rubyObject: RbObject

The Ruby object for the underlying array.

Declaration
Swift
public private(set) var rubyObject: RbObject { get }

Collection protocol conformance

init()

Create an empty collection - an empty Ruby array.

Declaration
Swift
public init()

var startIndex: Int

The position of the first element in a nonempty collection.

If the collection is empty, startIndex is equal to endIndex.

Declaration
Swift
public var startIndex: Int { get }

var endIndex: Int

The collection’s “past the end” position—that is, the position one greater than the last valid subscript argument.

When you need a range that includes the last element of a collection, use the half-open range operator (<) with endIndex. The < operator creates a range that doesn’t include the upper bound, so it’s always safe to use with endIndex. For example:

let numbers = [10, 20, 30, 40, 50]
if let index = numbers.firstIndex(of: 30) {
    print(numbers[index ..< numbers.endIndex])
}
// Prints "[30, 40, 50]"

If the collection is empty, endIndex is equal to startIndex.

Declaration
Swift
public var endIndex: Int { get }

subscript(index: Int) -> RbObject

Accesses the element at the specified position.

For example, you can replace an element of an array by using its subscript.

var streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
streets[1] = "Butler"
print(streets[1])
// Prints "Butler"

You can subscript a collection with any valid index other than the collection’s end index. The end index refers to the position one past the last element of a collection, so it doesn’t correspond with an element.

complexity

O(1)

Declaration
Swift
public subscript(index: Int) -> RbObject { get set }
Parameters
position

The position of the element to access. position must be a valid index of the collection that is not equal to the endIndex property.

func index(after: Int) -> Int

Returns the position immediately after the given index.

The successor of an index must be well defined. For an index i into a collection c, calling c.index(after: i) returns the same index every time.

Declaration
Swift
public func index(after i: Int) -> Int
Parameters
i

A valid index of the collection. i must be less than endIndex.

Return Value

The index value immediately after i.

func replaceSubrange<C>(Range<Int>, with: C)

Replaces the specified subrange of elements with the given collection.

This method has the effect of removing the specified range of elements from the collection and inserting the new elements at the same location. The number of new elements need not match the number of elements being removed.

In this example, three elements in the middle of an array of integers are replaced by the five elements of a > instance.

 var nums = [10, 20, 30, 40, 50]
 nums.replaceSubrange(1...3, with: repeatElement(1, count: 5))
 print(nums)
 // Prints "[10, 1, 1, 1, 1, 1, 50]"

If you pass a zero-length range as the subrange parameter, this method inserts the elements of newElements at subrange.startIndex. Calling the insert(contentsOf:at:) method instead is preferred.

Likewise, if you pass a zero-length collection as the newElements parameter, this method removes the elements in the given subrange without replacement. Calling the removeSubrange(_:) method instead is preferred.

Calling this method may invalidate any existing indices for use with this collection.

complexity

O(n + m), where n is length of this collection and m is the length of newElements. If the call to this method simply appends the contents of newElements to the collection, this method is equivalent to append(contentsOf:).

Declaration
Swift
public mutating func replaceSubrange<C>(
    _ subrange: Range<Int>, with newElements: C)
where C: Collection, C.Element: RbObjectConvertible
Parameters
subrange

The subrange of the collection to replace. The bounds of the range must be valid indices of the collection.

newElements

The new elements to add to the collection.