[RUBY] How to use arrays (personal memorandum)

Introduction

A summary of personal arrays.

Array definition

array = []

array1 = [1, 2, 3, 4, 5]

array2 = ["A", "B", "C"]

array3 = (1..5).to_a
=> [1, 2, 3, 4, 5]

array4 = ("a".."c").to_a
=> [a, b, c]

Add elements to the array

array = [1, 2, 3]

##Add to the beginning
array.unshift(10)
p array
=> [10, 1, 2, 3]

##Add element at the end
array << 10
p array
=> [1, 2, 3, 10]

array.push(10)
p array
=> [1, 2, 3, 10]

##Add element at specified position
array.insert(2, 5)
p array
=> [1, 2, 5, 3]

It is convenient because the insert method can be added at the specified position.

Delete array elements

array = [1, 2, 3, 4, 5]

##Delete the specified element
array.delete(4)
p array
=> [1, 2, 3, 5]

##Remove the first element
array.shift
p array
=> [2, 3, 4, 5]

##Remove last element
array.pop
p array
=> [1, 2, 3, 4]

##Delete the element at the specified position
array.delete_at(2)
p array
=> [1, 2, 4, 5]

##Delete a specified range of elements
array.slice!(1, 3) 
p array
=> [1, 5]

##Delete only true elements
array.select! { |n| n % 2 == 0 }
p array
=> [2, 4]

##Delete only false elements
array.reject! { |n| n % 2 == 0 }
p array
=> [1, 3, 5]

Array output

array = [1, 2, 3, 4, 5, 6, 7]

##Output at the specified position
puts array[0]
=> 1

puts array[5]
=> 6

##Output in the specified range
puts array.slice(3, 4).join
=> 4567

##Output only the first element of true
puts array.find { |n| n % 3 == 0 }
=> 3

##Output the position of the first element of true
puts array.find_index { |n| n % 3 == 0 }
=> 2

##Output only true elements
puts array.select { |n| n % 2 == 0 }.join
=> 246

##Output only false elements
puts array.reject { |n| n % 2 == 0 }.join
=> 1357

##false Output the previous element
puts array.take_while { |n| n < 5 }.join
=> 1234

##Output elements after false
puts array.drop_while { |n| n < 5 }.join
=> 567

At the end

I think you can do the basics with this.

Recommended Posts

How to use arrays (personal memorandum)
[Processing × Java] How to use arrays
A memorandum on how to use Eclipse
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to use Swift's Codable Super personal memo
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
How to use Lombok now
[Creating] How to use JUnit
[Rails] How to use Scope
How to use the link_to method