Basic methods of Ruby arrays

I will make a note of the basic methods used in Ruby arrays.

version ruby 2.6.5p114

Add / remove to array

First, prepare an appropriate array

$ animals = ["dog", "cat", "rabbit"]
=> ["dog", "cat", "rabbit"]
$ animals.push('bear')
=> ["dog", "cat", "rabbit", "bear"]
$ animals.unshift('monkey')
=> ["monkey", "dog", "cat", "rabbit", "bear"]

$ animals
=> ["monkey", "dog", "cat", "rabbit", "bear"]
$ animals.pop
=> "bear"
$ animals.shift
=> "monkey"
$ animals
=> ["dog", "cat", "rabbit"]

Array formatting

['dog',nil, 0, false,''].compact
=> ["dog", 0, false, ""]
["dog", "cat", "rabbit", "dog"].uniq
=> ["dog", "cat", "rabbit"]
$ animals = ["dog", ["cat",["rabbit, bear"]]]
=> ["dog", ["cat", ["rabbit, bear"]]]
$ animals.flatten
=> ["dog", "cat", "rabbit, bear"]
$ animals = [[1,2,3],["dog","cat","rabbit"]]
=> [[1, 2, 3], ["dog", "cat", "rabbit"]]
$ animals.transpose
=> [[1, "dog"], [2, "cat"], [3, "rabbit"]]
$ num = [1,2,3]
=> [1, 2, 3]
$ animals = ["dog","cat","rabbit"]
=> ["dog", "cat", "rabbit"]
$ num.zip(animals)
=> [[1, "dog"], [2, "cat"], [3, "rabbit"]]

Other useful methods

["dog", "cat", "rabbit"].size
=> 3
[1,2,3].sum
=> 6
[3,1,2].sort
=> [1, 2, 3]
[1,2,3].reverse
=> [3, 2, 1]
[1,2,3].shuffle
=> [2, 1, 3]
[1,2,3].sample
=> 3
[1,2,3].sample
=> 1
["dog", "cat", "rabbit"].join
=> "dogcatrabbit"
["dog", "cat", "rabbit"].join('When')
=> "dog, cat and rabbit"
num = [1,2,3,4,5,6]
=> [1, 2, 3, 4, 5, 6]
num.select {|n| n % 2 == 0}
=> [2, 4, 6]
> num
=> [1, 2, 3, 4, 5, 6]
num.reject { |n| n % 2 == 0 }
=> [1, 3, 5]

Recommended Posts

Basic methods of Ruby arrays
[Ruby] List of basic commands
Ruby methods and classes (basic)
Review of Ruby basic grammar
Basic knowledge of Ruby on Rails
Ruby basic terms
Ruby Learning # 13 Arrays
Basics of Ruby
About Ruby arrays
Ruby Learning # 15 Methods
About Ruby methods
[Ruby] Basic knowledge of class instance variables, etc.
[Java] Personal summary of classes and methods (basic)
Enumerate subsets of arrays given in Ruby (+ α)
Java review ③ (Basic usage of arrays / reference type)
Ruby learning points (basic)
ruby basic syntax memo
Ruby Joins multiple arrays
Ruby Learning # 31 Object Methods
About Ruby instance methods
Basic format of Dockefile
definition of ruby method
Ruby variables and methods
[Ruby] methods, instance methods, etc ...
ruby memorandum (basic edition)
[Ruby] The role of subscripts in learning elements in arrays
Java engineers now compare to learn the basic grammar of Ruby Part 2 (classes, methods)
Review the basic knowledge of ruby that is often forgotten
[Ruby] Various types of each
[Docker] Introduction of basic Docker Instruction
Ruby on Rails basic learning ①
Trap of asList of Arrays class
Three Bit Manipulation Methods (Ruby)
I tried to summarize the basic grammar of Ruby briefly
Super basic usage of Eclipse
What are Ruby class methods?
Judgment of fractions in Ruby
[Ruby] Singular methods and singular classes
[Ruby] Class methods, instance methods, etc.
Summary of basic functions of ImageJ
Ruby variables and functions (methods)
[Ruby] Find numbers in arrays
Ruby on Rails Basic Memorandum
[Ruby] Singular methods and singular classes
About the behavior of ruby Hash # ==
Understand the basic mechanism of log4j2.xml
Basics of sending Gmail in Ruby
Various methods of Java String class
Write Ruby methods using C (Part 1)
Implementation of ls command in Ruby
Extraction of "ruby" double hash * Review
[Ruby] See the essence of ArgumentError
Ruby standard input and various methods
Differences in split methods of StringUtils
The basic basis of Swift dialogs
Ruby convenience methods for fledgling engineers
The basic basis of Swift's Delegate
Basic usage of java Optional Part 1
[Ruby] Handle instance variables with instance methods
Various methods of the String class
Ruby 5 or higher sum of integers