Skip to content
instance method

Struct#values_at

(*::int | ::range[::int?] positions) -> ::Array[Elem]
values_at(*integers) → array
values_at(integer_range) → array

Returns an array of values from self.

With integer arguments integers given, returns an array containing each value given by one of integers:

Example
Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.values_at(0, 2)    # => ["Joe Smith", 12345]
joe.values_at(2, 0)    # => [12345, "Joe Smith"]
joe.values_at(2, 1, 0) # => [12345, "123 Maple, Anytown NC", "Joe Smith"]
joe.values_at(0, -3)   # => ["Joe Smith", "Joe Smith"]

Raises IndexError if any of integers is out of range; see Array Indexes at Array.

With integer range argument integer_range given, returns an array containing each value given by the elements of the range; fills with nil values for range elements larger than the structure:

Example
joe.values_at(0..2)
# => ["Joe Smith", "123 Maple, Anytown NC", 12345]
joe.values_at(-3..-1)
# => ["Joe Smith", "123 Maple, Anytown NC", 12345]
joe.values_at(1..4) # => ["123 Maple, Anytown NC", 12345, nil, nil]

Raises RangeError if any element of the range is negative and out of range; see Array Indexes at Array.

See also #values #to_s also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0