Ruby | Array values_at() function Last Updated : 06 Dec, 2019 Comments Improve Suggest changes Like Article Like Report Array#values_at() : values_at() is a Array class method which returns an array containing the elements in self corresponding to the given selector. Syntax: Array.values_at() Parameter: Array Return: an array containing the elements in self corresponding to the given selector Example #1 : Ruby # Ruby code for values_at() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, 50, 6] # values_at method example puts "values_at() method form : #{a.values_at(2, 4)}\n\n" puts "values_at() method form : #{b.values_at(1, 5)}\n\n" puts "values_at() method form : #{c.values_at(0, 3)}\n\n" Output : values_at() method form : [33, 5] values_at() method form : [4, 9] values_at() method form : [18, 6] Example #2 : Ruby # Ruby code for values_at() method # declaring array a = ["abc", "nil", "dog"] # declaring array c = ["cat", nil] # declaring array b = ["cow", nil, "dog"] # values_at method example puts "values_at() method form : #{a.values_at(0)}\n\n" puts "values_at() method form : #{b.values_at(2)}\n\n" puts "values_at() method form : #{c.values_at(1)}\n\n" Output : values_at() method form : ["abc"] values_at() method form : ["dog"] values_at() method form : [nil] Comment More infoAdvertise with us Next Article Ruby | Array values_at() function M mayank5326 Follow Improve Article Tags : Ruby Ruby-Methods Ruby Array-class Similar Reads Ruby | Array sample() function Array#sample() : sample() is a Array class method which returns a random element or n random elements from the array. Syntax: Array.sample() Parameter: Array Return: a random element or n random elements from the array. Example #1 : Ruby # Ruby code for sample() method # declaring array a = [18, 22, 1 min read Ruby | Array take() function Array#take() : take() is a Array class method which returns the number of elements in the array. Syntax: Array.take() Parameter: Array Return: the number of elements in the array. Example #1 : Ruby # Ruby code for take() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 1 min read Ruby | Array slice() function Array#slice() : slice() is a Array class method which returns a subarray specified by range of indices. Syntax: Array.slice() Parameter: Array Return: a subarray specified by range of indices. Example #1 : Ruby # Ruby code for slice() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring 1 min read Ruby | Array slice!() function Array#slice!() : slice!() is a Array class method which seletes the element(s) given by an index or by a range. Syntax: Array.slice!() Parameter: Array Return: Deletes the elements given by an index or by a range. Example #1 : Ruby # Ruby code for slice!() method # declaring array a = [18, 22, 33, n 2 min read Ruby | Array to_ary() function Array#to_ary() : to_ary() is a Array class method which returns self array representation. Syntax: Array.to_ary() Parameter: Array Return: self array representation. Example #1 : Ruby # Ruby code for to_ary() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 1 min read Ruby | Array select() function Array#select() : select() is a Array class method which returns a new array containing all elements of array for which the given block returns a true value. Syntax: Array.select() Parameter: Array Return: A new array containing all elements of array for which the given block returns a true value. Ex 2 min read Ruby | Array to_s() function Array#to_s() : to_s() is a Array class method which returns self array. Syntax: Array.to_s() Parameter: Array Return: self array Example #1 : Ruby # Ruby code for to_s() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, 50, 1 min read Ruby | Struct values_at() function The values_at() is an inbuilt method in Ruby that returns an array with the struct members values. Selector can be of two types: Integer or Range offset. Syntax: struct_name.values_at(range) Parameters: The function takes a single parameter range which will specify the start and end of the struct me 1 min read Ruby | Array assoc() function The assoc() function in Ruby is used to search through an array of arrays whose first element is compared with the index of the function and return the contained array if match found otherwise return either nil or vacant. Syntax: Array.assoc(Object) Here Array is the array of arrays. Parameters: Obj 2 min read Ruby | Array class to_s() function to_s() is an Array class method which returns the string representation of the array elements. Syntax: Array.to_s() Parameter: Array Return: string representation of the array elements. Example #1: Ruby # Ruby code for to_s() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = 1 min read Like