Arrays

Contents
Array literals
Read and write by index
Push pop shift
Iterate with each

Array literals

Square brackets build an array, with elements separated by commas. Elements may mix types, because an array holds references to objects, not fixed slots. The factory method Array.new builds a sized array filled with one default value.

nums = [1, 2, 3] mixed = ["Ada", 36, true] zeros = Array.new(3, 0) puts zeros.length

3

The last line prints 3, because zeros holds three copies of 0.

Read and write by index

Read an element with brackets and its position, starting at 0 for the first element. A negative index counts back from the end, so -1 is the last element. Reading a missing position returns nil, while assigning to a position replaces the value there.

names = ["Ada", "Bob", "Lin"] puts names[0] puts names[-1] names[1] = "Boris" puts names[1]

Ada
Lin
Boris

Push pop shift

The method push appends to the end, pop removes the last element and returns it, and shift removes the first element and returns it. The method length, with its alias size, tells how many elements are stored.

stack = [1, 2] stack.push(3) puts stack.length last = stack.pop puts last puts stack.length

3
3
2

Iterate with each

The method each hands every element to the block in order. When the position matters as well, each_with_index yields the element together with its index, starting at 0.

names = ["Ada", "Bob", "Lin"] names.each do |name| puts name end names.each_with_index do |name, i| puts "#{i}: #{name}" end

Ada
Bob
Lin
0: Ada
1: Bob
2: Lin

Next: Hashes

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: