RosettaCodeData/Task/Collections/Ruby/collections-1.rb

12 lines
404 B
Ruby
Raw Permalink Normal View History

2013-04-10 12:38:42 -07:00
# creating an empty array and adding values
2013-06-05 21:47:54 +00:00
a = [] #=> []
a[0] = 1 #=> [1]
a[3] = "abc" #=> [1, nil, nil, "abc"]
a << 3.14 #=> [1, nil, nil, "abc", 3.14]
2013-04-10 12:38:42 -07:00
# creating an array with the constructor
2013-06-05 21:47:54 +00:00
a = Array.new #=> []
a = Array.new(3) #=> [nil, nil, nil]
a = Array.new(3, 0) #=> [0, 0, 0]
a = Array.new(3){|i| i*2} #=> [0, 2, 4]