I had read through the subscripts, so I took this opportunity to summarize them.
Suffix
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.
From now on, it's an array bonus.
values_at
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.
last
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