Create populated in Ruby

Find this useful? Support us: Star on GitHub 6
Category: String | Language: Ruby

To create an array in Ruby, you can use square brackets [] and separate the elements with commas. Here are some examples:

1. An array of integers:

array_of_numbers = [1, 2, 3, 4, 5]

2. An array of strings:

array_of_colors = ["red", "green", "blue"]

3. An array of mixed data types:

mixed_array = [1, "hello", 2.5, true]

4. An empty array:

empty_array = []

You can also create an array using the Array.new method, like this:

new_array = Array.new(3, "hello")

This will create an array with 3 elements, all with the value "hello". You can replace "hello" with any value you want.