Ruby's array is basically used. Compared with arrays in C #, the least habit is to allow negative indexes (somewhat similar to Javascript)
Arr = [3, 4, 5, 6, 7, 8, 9] puts arr [0] #3 puts arr. first #3 puts arr [arr. length-1] #9 puts arr [arr. size-1] #9 puts arr. last #9 puts arr [-1] #9, subscript is-1, which is equivalent to taking the last element puts arr [-2] #8, take the second number as the result. Print arr [1 .. 3], "\ n" #456, take the print arr [-] and "\ n" # start with the last three elements of the subscript from 1 to 3, take two consecutive elements, namely, 7, 8.
The complex application is as follows. Here we should point out that arrays in Ruby have implemented stacks and queues by nature, which is thoughtful.
# From the E8.1-3 in "Ruby language getting started tutorial V1.0. rbarr = [4, 5, 6] print arr. join (","), "\ n" #4, 5, 6arr [4] = "M" # assign 5th elements to mprint arr. join (","), "\ n" # The 4th elements are empty because they are not assigned a value. The output result is 4, 5, 6, and mprint arr [3]. "\ n" # 4th elements are empty, so nilarr is output. delete_at (3) # Delete 4th elements print arr. join (","), "\ n" # output 4, 5, 6, Marr [2] = ["A", "B ", "C"] # assign the 3rd elements "6" to a one-dimensional array [a, B, c] puts arr [0] #4 puts arr [1] #5 puts arr [2] # A, B, C note: When puts is output, each element is automatically added to \ nputs arr [3] # mprint arr. join (","), "\ n" #4, 5, a, B, c, mprint arr [2], "\ n" # abcarr [0 .. 1] = [7, "H", "B"] # Replace the first two elements 4, 5 with 7, H, bprint arr. join (","), "\ n" #7, H, B, A, B, C, Marr. push ("B") # Press B to the end of the array print arr. join (","), "\ n" #7, H, B, A, B, C, M, Barr. delete (["A", "B", "C"]) # Delete the elements matching ["A", "B", "C"] in the array, that is, arr [2]. Note the preceding arr [2] = ["A", "B", "C"] print arr. join (","), "\ n" #7, H, B, M, Barr. delete ("B") # delete all B elements print arr. join (","), "\ n" #7, H, Marr. insert (2, "D") # Insert D to the first element. If there are other elements, the index moves print arr. join (","), "\ n" #7, H, D, Marr <"F" <2 # add element F, 2 to the end of the array, similar to the previous push usage print arr. join (","), "\ n" #7, H, D, M, F, 2arr. pop # An element pops up, which is combined with the previous push. The array in Ruby itself is a stack print arr. join (","), "\ n" #7, H, D, M, Farr. shift # shifts one element to the left, that is, the queue print arr is implemented. join (","), "\ n" # H, D, M, Farr. clear # Clear the print arr element. join (","), "\ n"
The last thing to note: When array each and delete are used together, unexpected results may occur sometimes!
# Arr = [1, [2, 3], 2, 3, 4, 5, 6] print arr. join (","), "\ n" #1, 2, 3, 2, 3, 4, 5, 6arr. delete ([2, 3]) print arr. join (","), "\ n" #1, 2, 3, 4, 5, 6arr. each {| x | arr. delete (x)}; print arr. join (","), "\ n" #2, 4, 6 Note: When the array object each and delete are used together, improper use will delete its "part" element.