Explanation about Array object of Ruby

What is an Array object?

A so-called "array", an object that can store multiple objects.

irb(main):039:0> [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]

Array operation

You can add an object at the end using the push method.

irb(main):040:0> arr = []
=> []
irb(main):041:0> arr.push(100)
=> [100]

There are other notations to add at the end, so I will introduce them.

irb(main):046:0> arr = []
=> []
irb(main):047:0> arr << 100
=> [100]

Assign an object to an Array

First, let's make an Array normally. Try storing the string enclosed in single quotes in an Array.

irb(main):052:0> arr = ['String1', 'String']
=> ["String1", "String"]
irb(main):053:0> arr
=> ["String1", "String"]

However, it is troublesome to write single quotes often, so there is a notation **% w% [] **.

irb(main):057:0> arr = %w[String1 String2]
=> ["String1", "String2"]
irb(main):058:0> arr
=> ["String1", "String2"]

You can see that the result is the same as before. In an array enclosed in **% w **, the elements do not need to be enclosed in single quotes All you have to do is separate them with a space.

Array processing

each method

You can retrieve each element of the array using the each method.

irb(main):064:1* arr.each do |i|
irb(main):065:1*   puts i
irb(main):066:0> end
=> [1, 2, 3, 4, 5]

map method

You can use the map method to create a new array by processing each element of the array. For example, to make an array in which each element is multiplied by "2", write like this.

irb(main):068:0> arr = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
irb(main):069:0> arr.map { |i| i*2 }
=> [2, 4, 6, 8, 10]

Multiple assignment

You can store each element of the array in a variable using the following notation.

irb(main):074:0> arr = [1, 2]
=> [1, 2]
irb(main):075:0> foo, bar = arr
=> [1, 2]
irb(main):076:0> foo
=> 1
irb(main):077:0> bar
=> 2

You can see that foo and bar each contain the 0th and 1st of the array.

Recommended Posts

Explanation about Array object of Ruby
Explanation about Ruby String object
About Ruby and object model
About the behavior of ruby Hash # ==
[Ruby] Array
[Ruby] About the difference between 2 dots and 3 dots of range object.
12 of Array
Sort an array of Ruby homebrew classes
About Ruby symbols
About ruby ​​form
About Ruby Hashes
Basics of Ruby
About Ruby arrays
About Ruby inheritance
Array is object
About ruby block
About Ruby Hashes
Ruby two-dimensional array
About object orientation
About Ruby Symbols
About Ruby variables
Ruby array manipulation
About Ruby methods
[Ruby] "Reference to object" and "Contents of variable"
About Ruby Kernel Module
About Ruby error messages
About Ruby exception handling
About disconnect () of HttpURLConnection
About Ruby Hashes (continued)
About eval in Ruby
[ruby] About here documents
About selection of OpenJDK
About Ruby if statement
Ruby Learning # 31 Object Methods
About Ruby instance methods
About DI of Spring ②
A note about th: field th: each th: object of thymeleaf
definition of ruby method
[Technical memo] About the advantages and disadvantages of Ruby
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
About form. ○○ of form_with
Explanation of Ruby on rails for beginners ② ~ Creating links ~
About the [ruby] operator
Thinking about logic Ruby
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
[Ruby] Questions and verification about the number of method arguments
A note about the seed function of Ruby on Rails
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
What is an immutable object? [Explanation of how to make]
Basic methods of Ruby hashes
About the handling of Null
[Ruby] Extracting a two-dimensional array
Basic methods of Ruby arrays
About simple operation of Docker
[Ruby] Various types of each
About the description of Docker-compose.yml
About size comparison of compareTo
About types of code coverage
[Ruby on Rails] about has_secure_password
Multiplication in a Ruby array
About regular expressions in Ruby