digits = 0..9
scale1 = 0..10
scale2 = 0...10 #digits = scale2
..
includes the final value, and ...
- no (stupid - it seemed that it should be the other way around). However, the ranges themselves are little used.a = Array.[](1,2,3,4)
b = Array[1,2,3,4]
c = [1,2,3,4]
[]
. There is also a new
method that takes 0, 1 or two parameters: the first is the number of elements, the second is their value. See an example:d = Array.new #
e = Array.new(3) # [nil, nil, nil]
f = Array.new(3, "ruby") # ["ruby", "ruby", "ruby"]
{}
):f[0].capitalize! # f : ["Ruby", "Ruby", "Ruby"]
g = Array.new(3) { "ruby" } # ["ruby", "ruby", "ruby"]
g[0].capitalize! # g : ["Ruby", "ruby", "ruby"]
[]
and []=
respectively. Each can accept one or two (beginning and length) parameters or a range. The countdown from the end of the array starts at -1
. Values ​​can also be delete_at
special simple at
method - it takes only one parameter and therefore works a little faster, delete_at
will delete the element. We look:a = [1, 2, 3, 4, 5, 6]
b = a[0] # 1
c = a.at(0) # 1
d = a[-2] # 5
e = a.at(-2) # 5
f = a[9] # nil
g = a.at(9) # nil
h = a[3,3] # [4, 5, 6]
i = a[2..4] # [3, 4, 5]
j = a[2...4] # [3, 4]
a[1] = 8 # [1, 8, 3, 4, 5, 6]
a[1,3] = [10, 20, 30] # [1, 10, 20, 30, 5, 6]
a[0..3] = [2, 4, 6, 8] # [2, 4, 6, 8, 5, 6]
a[-1] = 12 # [2, 4, 6, 8, 5, 12]
a.delete_at(3) # [1, 2, 4, 5, 6]
a.delete_at(9) # nil
join
method “merges” the elements of an array into one variable; we specify the separator as a parameter:x = []
x << "Word"
x << "Play" << "Fun"
puts x.join(', ') # Word, Play, Fun
first
and last
methods return the first and last elements of the array, respectively, and values_at
- an array containing only the selected elements:x = ["alpha", "beta", "gamma", "delta", "epsilon"]
a = x.first # alpha
b = x.values_at(0..2,4) # [alpha, beta, gamma, epsilon]
length
method and its size
alias return the number of elements in the array, and nitems
will not count empty elements ( nil
), compact
will remove nil
from the array altogether:y = [1, 2, nil, nil, 3, 4]
c = y.size # 6
e = y.nitems # 4
d = y.compact # [1, 2, 3, 4]
sort
(try it yourself). Sorting works only in arrays containing elements that can be compared - with arrays with mixed data types, the method will return an error. Sort the mixed array, converting its elements into strings on the fly (transform to to_s
method):a = [1, 2, "three", "four", 5, 6]
b = a.sort {|x,y| x.to_s <=> y.to_s}
# b [1, 2, 5, 6, "four", "three"]
<=>
is one of the comparison methods (see the third drop). The block returns -1
if the first element of the pair being compared is smaller (then the method swaps the elements), 0
if the elements are equal, 1
if more (in the last two cases, we leave the elements in place). Therefore, for a decreasing order, you simply swap the compared elements ( {|x,y| y.to_s <=> x.to_s}
). I think we understood everything.detect
( find
- its synonym) and find_all
( select
- the same); we find_all
expression into the block:x = [5, 8, 12, 9, 4, 30]
# find ,
x.find {|e| e % 6 == 0 } # 12
# select
x.select {|e| e % 6 == 0} # [12, 30]
reject
method is the reverse of select
— it will remove every element that satisfies the block:x = [5, 8, 12, 9, 4, 30]
d = c.reject {|e| e % 2 == 0} # [5, 9]
delete
deletes all elements containing specific data:c = ["alpha", "beta", "gamma", "delta"]
c.delete("delta")
# = ["alpha", "beta", "gamma"]
each
iterator is defined - it works simply, guess what this code does:[1, "test", 2, 3, 4].each { |element| puts element.to_s + "X" }
collect
iterator:[1, 2, 3, 4].collect { |element| element * 2 } #[2, 4, 6, 8]
x = [1, 2, 3]
y = ["a", "b", "c"]
z = x + y #[1, 2, 3, "a", "b", "c"]
a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
a — b # [1, 2] —
a & b # [3, 4] —
a | b # [1, 2, 3, 4, 5, 6] —
a*2 # [1, 2, 3, 4, 1, 2, 3, 4] -
Source: https://habr.com/ru/post/48859/
All Articles