[Ruby] The role of subscripts in learning elements in arrays

I had read through the subscripts, so I took this opportunity to summarize them.

The role of subscripts in arrays


a = [1,2,3,4,5,6,7]
a[2,5] #=> [3,4,5,6,7]

The first argument is the position The second argument represents the length.

By the way

Subscript substitution

a = [1,2,3,4,5,6,7]
a[2,5]= 777
a #=> [1,2,777]

From the 3rd element, 5 elements are replaced with 777 at once.

values_at and last

From now on, it's an array bonus.


a = [1,2,3,4,5,6,7]
a.values_at[2,5] #=> [3,6]

a = [1,2,3,4,5,6,7]
a.values_at[2,5,6] #=> [3,6,7]

You can add subscripts depending on the number of elements you want to get.


a = [1,2,3,4,5,6,7]
a.last  #=> 7

a = [1,2,3,4,5,6,7]
a.last(4)  #=> [4,5,6,7]

The method last, which retrieves the last element of an array, passes a value greater than or equal to 0 as an argument. The element is taken out from the back by that much. When I first saw it, I was wondering if I would get one element by counting the value specified in the argument from the back.

Even if you look only at the array, it's interesting because there are only new discoveries. Today is around here.

[Introduction to Ruby for professionals From language specifications to test-driven development / debugging techniques](https://www.amazon.co.jp/%E3%83%97%E3%83%AD%E3%] 82% 92% E7% 9B% AE% E6% 8C% 87% E3% 81% 99% E4% BA% BA% E3% 81% AE% E3% 81% 9F% E3% 82% 81% E3% 81% AERuby% E5% 85% A5% E9% 96% 80-% E8% A8% 80% E8% AA% 9E% E4% BB% 95% E6% A7% 98% E3% 81% 8B% E3% 82% 89 % E3% 83% 86% E3% 82% B9% E3% 83% 88% E9% A7% 86% E5% 8B% 95% E9% 96% 8B% E7% 99% BA% E3% 83% BB% E3 % 83% 87% E3% 83% 90% E3% 83% 83% E3% 82% B0% E6% 8A% 80% E6% B3% 95% E3% 81% BE% E3% 81% A7-Software-Design -plus% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA / dp / 4774193976)

Recommended Posts

[Ruby] The role of subscripts in learning elements in arrays
[Ruby basics] About the role of true and break in the while statement
The story of learning Java in the first programming
Enumerate subsets of arrays given in Ruby (+ α)
Count the number of occurrences of a string in Ruby
Examine the elements in the array using the [Ruby] includes? Method
Get the URL of the HTTP redirect destination in Ruby
Summarize the additional elements of the Optional class in Java 9
Basic methods of Ruby arrays
Judgment of fractions in Ruby
[Ruby] Find numbers in arrays
Determine that the value is a multiple of 〇 in Ruby
About the behavior of ruby Hash # ==
[Java] Delete the elements of List
Basics of sending Gmail in Ruby
[Ruby] See the essence of ArgumentError
Defeat the hassle of treating C arrays as Tuples in Swift
About the role of the initialize method
Order of processing in the program
I want to change the value of Attribute in Selenium of Ruby
[Ruby] Display the contents of variables
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
I tried to make full use of the CPU core in Ruby
How to insert processing with any number of elements in iterative processing in Ruby
Extract elements of array / extract in reverse order-java
Acquisition of article information in ruby ​​scraping
Examine the memory usage of Java elements
Directory information of DEFAULT_CERT_FILE in Mac ruby 2.0.0
The identity of params [: id] in rails
Implement the algorithm in Ruby: Day 1 -Euclidean algorithm-
part of the syntax of ruby ​​on rails
Summary of hashes and symbols in Ruby
Compare the elements of an array (Java)
The story of AppClip support in Anyca
[Ruby] Cut off the contents of twitter-ads
Ruby from the perspective of other languages
[Ruby] Classification and usage of loops in Ruby
The story of writing Java in Emacs
Role of JSP in Web application [Java]
Write the movement of Rakefile in the runbook
[Swift] How to get the number of elements in an array (super basic)
Ruby: Nokogiri automatically determines the character code of html read in binary mode
Ruby learning 4
Use hashes well in Ruby to calculate the total amount of an order
Ruby learning 5
Ruby learning 3
Call a method of the parent class by explicitly specifying the name in Ruby
Ruby learning 2
TECH CAMP (Engineer career change) Review of learning contents in the 6th week
Ruby learning 6
Ruby learning 1
TECH CAMP (Engineer career change) Review of learning contents in the 4th week
Learning Ruby with AtCoder 9 [1st Algorithm Practical Test 3rd] Sorting of array elements
Summary of how to select elements in Selenium
[Order method] Set the order of data in Rails
How to find the cause of the Ruby error
[Ruby] Summary of class definitions. Master the basics.
The story of low-level string comparison in Java
The story of making ordinary Othello in Java
[Ruby on Rails] Until the introduction of RSpec
About the idea of anonymous classes in Java