1, the definition of the array
The array in Ruby is a dynamic array, the stored data is not qualified type, the length of the array is dynamically extended according to the storage needs, so, in the data definition, only the simplest way to new an array object, you can use the following methods:
Copy Code code as follows:
Arr1=[] #最简单的Array创建方式
Arr2=array.new #标准的Array创建方式
Arr3=%w[this is a example!] #%w method converts an established string to an array
Arr4= (1..10). To_a #其他集合对象转换为数组
2, access to array elements
Array object access is done through array subscripts, with the subscript starting at 0 to the array length-1, the negative number representing the index starting at the end of the array, and the first digit representing the starting position, and the second digit representing the start bit
Number of elements to set up, there are special ways to access the array elements, as shown in the following sample code:
Copy Code code as follows:
Arr= (1..10). to_a
Puts Arr[2] #输出: 3
Puts Arr.first #输出: 1
Puts Arr.last #输出: 10
Puts Arr[3..5] #输出: 4 5 6, returns a sub array of subscript 3-5
Puts Arr[-1] #输出: 10
Puts arr[0,3] #输出: 1 2 3, returns a sub array with the subscript starting at 0, length 3
Puts arr[-5,3] #输出: 6 7 8, return subscript starting with-5, with a length of 3 sub array
3, the operation of the array
Ruby arrays are dynamically developed, can be placed in the same array of different objects, add and delete elements when the length of automatic change, the following sample code:
Copy Code code as follows:
Arr= (1..5). To_a #数组: 1 2 3 4 5
Arr[1]=time.new #数组: 1 2013-03-01 11:19:48 +0800 2 3 4 5
Arr.push (' Hello ') #数组: 1 2013-03-01 11:19:48 +0800 2 3 4 5 ' Hello '
arr<< ' World ' #数组: 1 2013-03-01 11:19:48 +0800 2 3 4 5 ' Hello ' world ', equivalent push
Arr.insert (2, ' Hi ') #数组: 1 2013-03-01 11:19:48 +0800 ' Hi ' 2 3 4 5 ' Hello ' ' World '
Arr.delete (' Hi ') #数组: 1 2013-03-01 11:19:48 +0800 2 3 4 5 ' Hello ' World '
Arr.delete_at (1) #数组: 1 2 3 4 5 ' Hello ' ' World '
Arr.shift #数组: 2 3 4 5 ' Hello ' World ', deleted the first element
Arr.pop #数组: 2 3 4 5 ' Hello ', delete the tail element
Arr.clear #数组: Empty array
#支持将数组放入原数组, as an element exists, such as:
Arr= (1..5). To_a #数组: 1 2 3 4 5
Arr.push ([' Hello ', ' Hi ']) #数组: 1 2 3 4 5 [' Hello ' ' Hi ']
arr<<[' time ', ' Date '] #数组: 1 2 3 4 5 [' Hello ' ' Hi '] [' time ' ' date ']
Arr.delete ([' Hello ', ' Hi ']) #数组: 1 2 3 4 5 [' Time ' ' Date ']
Arr.insert (2,[' A ', ' B ', ' C ']) #数组: 1 2 [' a ' B ' ' C '] 3 4 5 [' Time ' ' Date ']
#注意: [' A ', ' B ', ' in the top code] C ' and [' time ', ' Date '] in the array arr is the whole, in the arr of the length of 1, the bottom of this way is not the same, [' A ', ' B ', ' C '] will become arr in the 3 elements
Arr[1..3]=[' A ', ' B ', ' C '] #数组: 1 A B C 4 5 [' Time ' ' Date ']
4, the operation of the array
The Ruby array supports operations such as the + 、-、 *, |, & Operators, + "The result is a synthesis of all the elements of the 2 arrays, the"-"operation result is the left array of elements that do not exist in the right array, the" * "operator to the right of the integer type, get the original array of x-times array," | " For the combined operation, a set of all elements of the 2 array is obtained, unlike "+", "|" There are no duplicate elements in, "&" is the intersection operation, get the collection of elements that exist in 2 arrays, similarly, there is no duplicate element, the following sample code:
Copy Code code as follows:
arr1=[1,2,3, ' A ', ' B '
arr2=[1,2, ' A ', ' C ', ' d ']
I=ARR1+ARR2 #数组: 1 2 3 a B 1 2 a C D
J=ARR1-ARR2 #数组: 3 b
K=arr1*2 #数组: 1 2 3 a B 1 2 3 a B
M=ARR1|ARR2 #数组: 1 2 3 a B c D
N=ARR1&ARR2 #数组: 1 2 A
5. Other
There are other convenient methods for the array, such as inversion, sorting, whether to include an element, etc., you can refer to the official api:http://ruby-doc.org/core-2.0/array.html