Ruby Array (Array) learning notes, rubyarray

Source: Internet
Author: User
Tags array definition

Ruby Array (Array) learning notes, rubyarray

1. array Definition

Arrays in Ruby are dynamic arrays, And the stored data does not have to be of a limited type. The length of the array is dynamically extended according to the storage requirements. Therefore, when defining data, you only need to use the simplest method to create an Array object. You can use the following methods:
Copy codeThe Code is as follows:
Arr1 = [] # simplest method of creating an Array
Arr2 = Array. new # standard Array Creation Method
Arr3 = % w [This is a example!] # % W convert an existing string to an array
Arr4 = (1... 10). to_a # convert other collection objects to Arrays

2. Access to array elements

The array object is accessed through the array subscript. The subscript starts from 0 to the array length-1. A negative number indicates the index starting from the end of the array. An array is indexed with a number, the first digit indicates the starting position, and the second digit indicates the starting position.

In addition, there are some special ways to access the array elements, as shown in the following sample code:
Copy codeThe Code is as follows:
Arr = (1... 10). to_a
Puts arr [2] # output: 3
Puts arr. first # output: 1
Puts arr. last # output: 10
Puts arr [3 .. 5] # output: 4 5 6, returns a subarray with a subscript of 3-5.
Puts arr [-1] # output: 10
Puts arr [] # output: 1 2 3, returns a subarray with a subscript starting from 0 and a length of 3.
Puts arr [-5, 3] # output: 6 7 8, returns a subarray with a subscript starting from-5 and a length of 3.

3. Array Operations

Ruby arrays are dynamically developed. Different objects can be placed in the same array. The length of the added or deleted elements is automatically changed. The following sample code:
Copy codeThe Code is as follows:
Arr = (1 .. 5). to_a # array: 1 2 3 4 5
Arr [1] = Time. new # array: 1 11:19:48 + 0800 2 3 4 5
Arr. push ('hello') # array: 1 11:19:48 + 0800 2 3 4 5 'hello'
Arr <'World' # array: 1 11:19:48 + 0800 2 3 4 5 'hello' 'World', equivalent to push
Arr. insert (2, 'Hi') # array: 1 11:19:48 + 0800 'Hi' 2 3 4 5 'hello' 'World'
Arr. delete ('Hi') # array: 1 11:19:48 + 0800 2 3 4 5 'hello' 'World'
Arr. delete_at (1) # array: 1 2 3 4 5 'hello' 'World'
Arr. shift # array: 2 3 4 5 'Hello ''', removing the first element
Arr. pop # array: 2 3 4 5 'Hello'. The last element is deleted.
Arr. clear # array: empty array
# You can add an array to the original array as an element, for example:
Arr = (1 .. 5). to_a # array: 1 2 3 4 5
Arr. push (['hello', 'Hi']) # array: 1 2 3 4 5 ['hello' 'Hi']
Arr <['time', 'date'] # array: 1 2 3 4 5 ['hello' 'Hi'] ['time' 'date']
Arr. delete (['hello', 'Hi']) # array: 1 2 3 4 5 ['time' 'date']
Arr. insert (2, ['A', 'B', 'C']) # array: 1 2 ['A' B 'C'] 3 4 5 ['time' 'date']
# Note: ['A', 'B', 'C'] and ['time', 'date'] in the above Code exist in the array arr, the length in arr is 1. The following method is different. ['A', 'B', 'C'] will become three elements in arr.
Arr [1 .. 3] = ['A', 'B', 'C'] # array: 1 a B c 4 5 ['time' 'date']

4. Array Operations

The Ruby array supports the +,-, *, |, and & operators. The "+" operation results in the synthesis of all elements of the two arrays, the "-" operation result is an array on the left that does not exist in the array on the right, and the "*" operator is of the integer type on the right. The result is an array x times that of the original array, "|" is the union operation. The set of all elements in the two arrays is obtained. Different from "+", "|" does not contain duplicate elements, and "&" is an intersection operation, we can get a set of elements that exist in two arrays. Similarly, there are no repeated elements. The following sample code:
Copy codeThe Code is as follows:
Arr1 = [1, 2, 3, 'A', 'B']
Arr2 = [1, 2, 'A', 'C', 'D']
I = arr1 + arr2 # array: 1 2 3 a B 1 2 a c d
J = arr1-arr2 # array: 3 B
K = arr1 * 2 # array: 1 2 3 a B 1 2 3 a B
M = arr1 | arr2 # array: 1 2 3 a B c d
N = arr1 & arr2 # array: 1 2

5. Others

Array also has some other very convenient methods, such as reverse, sorting, whether to contain a certain element, can refer to the official API: http://ruby-doc.org/core-2.0/Array.html


Ruby array storage value

Why array?
You can use the gets function to obtain the keyboard input. You can go to irb for an interview:
Puts "I am # {puts }"

For example:
Names = []
Names. push (gets)
In this way, the input content becomes the first element in the array. Is that what you want?

Ruby array Problems

A = [] # a = Array. new

10. times do
Input = gets. chomp
A <input. to_ I
End

# A. each do | j |
# Print j. to_s, "\ t"
# End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.