About Ruby arrays

Array

Arrays can hold multiple values. Variables can only hold one value.

Arrays can be used by enclosing multiple values in parentheses [], such as array name = ["value 1 "," value 2 "," value 3 "]. Each value is separated by a comma (,).

Benefits of using arrays

The method of preparing an array and storing the values has the following advantages.

--No need to define variables every time --The code is easier to read --The amount of code written is reduced

Not only can an array hold multiple values, but you can also assign those values to variables and output their contents.

(Example)

names = ["sawamura", "yamada", "tanaka"]
puts names
#=> sawamura
#   yamada
#   tanaka

As shown above, the values stored in the array are displayed in order.

Get elements of an array

The values stored in the array are numbered in order. This assigned number is called the ** index number **, and the value contained in the array is called the ** element **.

To retrieve a specific element, use the following syntax.

Array name [index number] (Example)

names = ["sawamura", "yamada", "tanaka"]
puts names[0]
#=> sawamura

Add elements to the array

If you want to add an element to the array, you can add it with <<.

names = ["sawamura", "yamada", "tanaka"]
names << "sato"
puts names
#=> sawamura
#   yamada
#   tanaka
#   sato

push method

The push method, like the << method, adds a new element to the end of the array. The difference is that you can add only one element to the array with the << method, but you can add multiple elements at once with push.

(Example)

names = ["sawamura", "yamada", "tanaka"]
names.push("sato","wakabayashi")
puts names
#=> sawamura
#   yamada
#   tanaka
#   sato
#   wakabayashi

Remove elements from array

shift method

The shift method is a method that deletes the first element of the array.

(Example)

names = ["sawamura", "yamada", "tanaka"]
names.shift
puts names
#=> yamada
#   tanaka

#index[0]confirm
#The index has also been updated
puts names[0]
#=> yamada

Summary

--Each array has an index that represents the element number --Use a form like names [0] to specify the index of the array and retrieve the element. --If you want to add an element to the array, use << to add it

Recommended Posts

About Ruby arrays
Ruby Learning # 13 Arrays
About Ruby symbols
[Java] About arrays
About ruby ​​form
About Ruby Hashes
About Ruby inheritance
About ruby block
About Java arrays
About Ruby Hashes
About Ruby Symbols
About Ruby variables
About Ruby methods
About Ruby Kernel Module
Java, about 2D arrays
About Ruby error messages
About Ruby exception handling
About eval in Ruby
[ruby] About here documents
About Ruby if statement
About Ruby instance methods
[Ruby] About instance generation
About the [ruby] operator
Thinking about logic Ruby
Basic methods of Ruby arrays
Explanation about Ruby Range object
A rough note about Ruby arrays and hash objects
[Ruby on Rails] about has_secure_password
About regular expressions in Ruby
About Ruby hashes and symbols
Ruby About various iterative processes
About =
About Ruby and object model
About Ruby classes and instances
[Ruby] Find numbers in arrays
Explanation about Ruby String object
About the behavior of ruby Hash # ==
About Ruby single quotes and double quotes
About Ruby product operator (&) and sum operator (|)
[Super Introduction] About Symbols in Ruby
About object-oriented inheritance and about yield Ruby
[Ruby] Review about nesting of each
Explanation about Array object of Ruby
About method.invoke
Ruby learning 4
About Kotlin
[Ruby] Array
About Hinemos
Ruby basics
About inheritance
About params
About Docker
Ruby Review 2
Ruby addition
Refactoring Ruby
About Rails 6
About form_for
[Ruby on Rails] About bundler (for beginners)
Ruby learning 3
About enum
About polymorphism