Creating Arrays
Part of the Fundamentals section of Coddy's Ruby journey — lesson 58 of 88.
An array is an ordered collection of elements. Think of it as a list that can hold multiple values in a single variable. Arrays are incredibly useful when you need to work with groups of related data.
To create an array in Ruby, use square brackets [] with elements separated by commas:
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "banana", "cherry"]
puts fruits # Outputs each fruit on a new lineRuby arrays can hold any type of data, and you can even mix different types in the same array:
mixed = [42, "hello", true, 3.14]You can also create an empty array and add elements later:
empty_list = []To check how many elements an array contains, use the length or size method:
colors = ["red", "green", "blue"]
puts colors.length # Outputs: 3Arrays maintain the order of their elements, so the first item you add stays first, the second stays second, and so on. This ordering becomes important when we learn to access individual elements in the next lesson.
Challenge
EasyRead three inputs:
- A string (first element)
- An integer (second element)
- A string (third element)
Create an array containing these three elements in the order they were received. Then print the array using puts, followed by the array's length on a new line.
For example, if the inputs are apple, 42, and banana, the output should be:
apple
42
banana
3If the inputs are red, 100, and blue, the output should be:
red
100
blue
3If the inputs are hello, 7, and world, the output should be:
hello
7
world
3Cheat sheet
An array is an ordered collection of elements that can hold multiple values in a single variable.
Create an array using square brackets [] with elements separated by commas:
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "banana", "cherry"]Arrays can hold any type of data, including mixed types:
mixed = [42, "hello", true, 3.14]Create an empty array:
empty_list = []Print an array using puts (outputs each element on a new line):
puts fruitsGet the number of elements using length or size:
colors = ["red", "green", "blue"]
puts colors.length # Outputs: 3Try it yourself
# Read inputs
first_element = gets.chomp
second_element = gets.to_i
third_element = gets.chomp
# TODO: Write your code below
# Create an array with the three elements
# Print the array using puts, then print the array's lengthThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False