×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby Array Methods

By IncludeHelp Last updated : November 17, 2024

Array Methods

The following are the methods of Ruby Array class:

Method Description
Array.each Iterates over each element, executing the block for each.
Array.reverse Returns a new array with elements in reverse order.
Array.reverse! Reverses the array in place.
Array.reverse_each Iterates over elements in reverse order.
Array.map and Array.map! Transforms each element using the block; map! modifies the array in place.
Array.reject Returns a new array excluding elements for which the block returns true.
Array.reject! Modifies the array to exclude elements for which the block returns true.
Array.select Returns a new array containing elements for which the block returns true.
Array.select! Modifies the array to include only elements for which the block returns true.
Array.drop_while Drops elements until the block returns false and returns the rest.
Array.delete_if Removes elements for which the block returns true.
Array.keep_if Keeps elements for which the block returns true, modifying the array.
Array.assoc(obj) Finds the first array element with obj as its first element.
Array.include?(obj) Checks if the array contains obj.
Array.collect Alias for Array.map.
Array.combination() Returns all combinations of length n from the array.
Array.permutation() Returns all permutations of length n from the array.
Array.product() Combines elements with elements from other arrays.
Array.rotate() Rotates elements by n positions, returning a new array.
Array.rotate! Rotates elements in place by n positions.
Array.index() Returns the index of the first occurrence of obj.
Array.rindex() Returns the index of the last occurrence of obj.
Array.each_index Iterates over array indices.
Array.insert() Inserts an object at the specified index.
Array.fill(obj) Fills the array with a given value.
Array.fill(obje, range) Fills specific indices with a value.
Array.fill(start [, length] ) { |index| block }) Fills the array with values based on a block.
Array.flatten Returns a new array with nested arrays flattened.
Array.replace() Replaces the contents of the array with another array.
Array.cycle() Iterates over elements repeatedly n times, executing the block.
Array.join() Converts the array to a string, with elements separated by sep.
Array.sample() Returns a random element or n random elements.
Array.rassoc() Finds the first array element where the second value matches obj.
Array.rassoc(obj) Returns a new array with elements shuffled.
Array.shuffle Shuffles the array in place.
Array.shuffle! Returns the first n elements.
Array.take() Returns the first n elements.
Array.take_while Returns elements while the block returns true.
Array.repeated_combination() Returns repeated combinations of length n.
Array.repeated_permutation() Returns repeated permutations of length n.
Array.shift Removes and returns the first element.
Array.slice() Returns a subarray based on index and length.
Array.slice!() Removes and returns the subarray at the index.
Array.sort Returns a new array with elements sorted.
Array.sort! Sorts the array in place.
Array.sort_by Sorts based on a block, returning a new array.
Array.sort_by! Sorts the array in place based on a block.
Array.uniq Returns a new array with duplicate values removed.
Array.uniq! Removes duplicates in place.
Array.transpose Transposes rows and columns in a 2D array.
Array.unshift() Prepends an object to the array.
Array.values_at() Returns elements at specified indices.
Array.zip() Combines corresponding elements from arrays into subarrays.
Array.pack() Encodes array elements into a binary sequence.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.