OneBite.Dev - Coding blog in a bite size

declare an array in Ruby

Code snippet on how to declare an array in Ruby

  array_name = [item1, item2, item3]

This code creates an array called ‘array_name’, which stores the items ‘item1’, ‘item2’, and ‘item3’. An array is a type of data structure in which multiple pieces of data can be stored, and it can be referenced by its name. The items are placed within square brackets and are separated by commas. In this example, the array is being created with three items already in it, but additional items can be added later if needed. This code is useful if the items are predetermined and the array will only hold those items, or if the items aren’t known beforehand and can change over time.

ruby