Learn Ruby Array

wang xiao bo 's photo
wang xiao bo

Published on Sep 1, 2021

1 min read

Subscribe to my newsletter and never miss my upcoming articles

//An array holds a number of items. In this example, the array is holding things I like.

things_i_like = ["books", "teat", "movies"]

things_i_like.each do |thing|
  puts "I like " + thing
end

//an array full of numbers. We need to print the double of each number in that array.
numbers = [2,4, 6, 8]
numbers.each do |item|
   puts item * 2
end

//Reverse an Array
things_i_like = ["books", "teat", "movies"]
reversed_array = things_i_like.reverse

reversed_array.each do |thing|
  puts "I like " + thing
end

//Get Item From a Position in Array
things_i_like = ["books", "teat", "movies"]
puts things_i_like[1] // teat
 
Share this