1. Creating an array
A=array.new (6,obj=nil) #=> [nil, nil, nil, nil, nil, nil] set default values
A=array.new (6) #=> [Nil, nil, nil, nil, nil, nil] The default value is nil
A=array.new (6,obj=1) {|obj| obj+1} #=> [1, 2, 3, 4, 5, 6] default values for the iteration inside the block
A=%w{1 2 3 4 5 6} #=> [1, 2, 3, 4, 5, 6]
A=[1, 2, 3, 4, 5, 6] #=> [1, 2, 3, 4, 5, 6]
2. Array manipulation
Require ' iconv '
methods=[
["A", "array"],
[' a.length ', ' length '],
[' a[-1] ', ' Countdown to the first '],
[' a[10] ', ' the subscript that exceeds the length of the array denotes nil '],
[' a[2,3] ', ' indicates a row of 3 array segments starting from 2 '],
[' a[1..3] ', ' denotes an array segment labeled Subscript 1 to Subscript 3 '],
[' a[1..3] ', ' means an array of subscripts from 1 to subscript 2, not containing the last number '],
[' a.delete_at (0) ', ' delete the first element '],
[' A.insert (0, 1) ', ' Insert 1 at the beginning of the array '],
[' a.slice! (1, 2) ', ' (slice) means that the 2 elements that get the start of subscript 1 are not deleted, and (slice!) Delete the element after getting it.
[' a<<7 ', ' added to the end of the array with A.push (7) '],
[' A.pop (2) ', ' Remove 2 elements at the end of the same a.slice! ( -1,2) '],
[' A.shift (2) ', ' remove the previous two elements with a.slice! (0,2) '],
[' A.unshift ', ' before inserting two elements with A.insert (0, +) '],
[' A=[0]+a ', ' [(+ <<, and the set contains duplicates] (|, the set does not contain duplicates)] [&, intersection] [-, difference set] '],
]
Methods.each {|method| puts "#{method[0]} = #{eval (Method[0])} # #{iconv.conv (" GB18030 "," UTF-8 ", Method[1])}"}
In order to achieve the output of this format "a = 123456 # array" Without each formatting, all can take advantage of Ruby's eval dynamic execution code, because there is Chinese so the use of
Iconv.conv method Transcode.
The output is as follows
A = 123456 # array
A.length = 6 # length
A[-1] = 6 # The first one to count
A[10] = # exceeds the length of the array subscript denotes nil
a[2,3] = 345 # Indicates a continuous 3 array segment with subscript starting from 2
A[1..3] = 234 # indicates an array segment with subscript from 1 to subscript 3
A[1..3] = 234 # indicates an array segment with subscript from 1 to subscript 2, not containing the last number
A.delete_at (0) = 1 # Delete the first element
A.insert (0, 1) = 123456 # insert 1 at the beginning of the array
a.slice! (1, 2) = + # (slice) means that the 2 elements that get the start of subscript 1 are not deleted, and the elements are deleted after the (slice!) gets finished
A<<7 = 14567 # added to the end of the array with A.push (7)
A.pop (2) = 67 # Delete End 2 elements same as a.slice! ( -1,2)
A.shift (2) = 14 # Remove the previous two elements with a.slice! (0,2)
A.unshift = 125 # Front insert two elements with A.insert (0, +)
A=[0]+a = 0125 # [(+ <<, and set contains Duplicates) (|, and set does not contain duplicates)] [&, intersection] [-, difference set]
Ruby-array Array