how to use [1..4] to generate an array in ruby

You can use the range syntax [1..4] in Ruby to generate an array.

Here is an example:

main.rb
array = (1..4).to_a
puts array.inspect
39 chars
3 lines

Output:

main.rb
[1, 2, 3, 4]
13 chars
2 lines

In the above example, we create a range from 1 to 4 using the syntax (1..4). Then, we convert the range to an array using the to_a method. Finally, we print the array using inspect. The resulting array is [1, 2, 3, 4].

related categories

gistlibby LogSnag