Skip to content

struct Slice(T)
inherits Struct #

A Slice is a Pointer with an associated size.

While a pointer is unsafe because no bound checks are performed when reading from and writing to it, reading from and writing to a slice involve bound checks. In this way, a slice is a safe alternative to Pointer.

A Slice can be created as read-only: trying to write to it will raise. For example the slice of bytes returned by String#to_slice is read-only.

Included modules

Comparable Indexable::Mutable

Methods#

#to_tensor(device = CPU, shape : Array(Int32)? = nil) #

Converts a standard library slice to a Tensor. The type of Tensor is inferred from the element type, and alternative shapes can be provided.

Arguments#
  • device : Num::Storage - The storage backend on which to place the Tensor
  • shape : Array(Int32)? - An optional shape this slice represents
Examples#
s = Slice.new(200) { |i| (i + 10).to_u8 }
typeof(s.to_tensor) # => Tensor(UInt8, CPU(Float32))
View source