Arrays in Ruby are ordered collections of any object. They are one of the most commonly used data structures in Ruby and provide a way to store multiple items in a single variable. Arrays can contain any type of data, including numbers, strings, and other arrays.
numbers = [1, 2, 3]
alphabet = ["a", "b", "c"]
mixed_array = [1, 2, "abc", "xyz", 23.0]
all_arrays = [numbers, alphabet, 1, 2, nil]
# Return array size
[1, 2, 3].size #=> 3
[1, 2, 3].length #=> 3
[1, 2, 3].count #=> 3
# Get rid of nil values
[1, 2, 3, nil].compact #=> [1, 2, 3]
# Returns a new array with duplicate values removed
[1, 2, 3, 3, 2].uniq #=> [1, 2, 3]
# Removes all elements from the array
[1, 2, 3].clear #=> []
# Returns a new array containing the elements of the original array in reverse order
[1, 2, 3].reverse #=> [3, 2, 1]
# Return boolean value depends on the result
[1, 2, 3].include?(2) #=> true
[1, 2, 3].include?(0) #=> false
# Returns a string created by converting each element of the array to a string, separated by the given separator
[1, 2, 3].join(", ") # => "1, 2, 3"
[1, 2, 3].join("-") # => "1-2-3"
# Returns a new array that is a one-dimensional flattening of the original array.
[[1, 22, 333], "a", "b"].flatten #=> [1, 22, 333, "a", "b"]