Ruby Array in instance application

Source: Internet
Author: User
Tags arrays


Array



1. To create an array:



As with many other programming languages, it is easy to create an array, but note that Ruby is a weak type of scripting language in which the types of elements in an array can be different and the length of the array variable:


The code is as follows

arr = ["One", "two", "three"]

OR

arr = array.new #an empty Array

arr = array.new (5) #[nil, nil, nil, nil, nil]

arr = array.new (5, "Hello") #["Hello", "Hello", "Hello", "Hello", "Hello"]



When creating an array, if its elements are of type string and use single quotes, and you do not want to enter single quotes, then we can use the following method to create (use%w to create strings enclosed in double quotes):


The code is as follows

arr =% w (this was an array of strings) #use space as element separator



2. Accessing the value of an array by index:



In Ruby, an array index can be a positive integer (from the front to the beginning, starting at 0) or it can be a negative integer (from the back to the front, starting from-1) can also be a range (range):



The code is as follows


arr = [' h ', ' e ', ' l ', ' l ', ' o ', ', ' w ', ' O ', ' r ', ' L ', ' d ']

Print arr[0,5]

Puts

Print arr[0..4]

Puts

Print arr[0...4]

Puts

Print arr[-5,5]

--------Results--------

Hello

Hello

Hell

World



note:arr[-5,5]: The previous integer-5 indicates the index of the start, followed by an integer 5 to indicate the quantity






3. Copy array:



When an array variable is assigned to another array variable using "=", the two array variables refer to the same array, modify the value of one of the arrays, and the other is modified. We can use the Clone method to copy a new array:


The code is as follows

arr = ['One', 'two', 'three',]

arr1 = arr #arr and arr1 refer to the same array, modifying one another will also modify

Arr2 = arr.clone # arr2 is a new version of arr, modifying arr2 does not affect arr





4. Compare Array Size:



When using <=> to compare two arrays of A1 and A2, there are three scenarios:



1. A1 less than A2, return-1



2. A1 equals A2, return 0



3. A1 greater than A2, return 1



So, how is the array larger than the size?



When two arrays are compared, the values of the two arrays (starting at 0) are compared, as [0,10,20]<=> [0,20,20] returns-1, because when the index is 1 o'clock,10<20;






In addition, if the elements of the two arrays that are compared are strings, then their ASCII is compared, and if an array is longer and the other elements are the same, then the number leader's ' big '.



However, if an element in a short array is larger than the same position within another long array, the short array is considered ' large '.


The code is as follows

arr1=[1,2,3,4,5]

arr2=[1,2,3,4,5]

arr3=[1,2,3,4]

arr4=[1,2,3,5]

ARR5=[1000,2000,3000]

arr6=[1,2]

arr7=[2,1]

arr8=["Hello", "World"]

arr9=["Hello", "World"]



P (ARR1<=>ARR2) # 0

P (ARR3<=>ARR1) #-1

P (ARR4<=>ARR3) # 1

P (ARR5<=>ARR4) # 1

P (ARR7<=>ARR6) # 1

P (ARR8<=>ARR7) # nil

P (ARR9<=>ARR8) #-1




5. Array Ordering:



The Sort method uses the comparison operator <=> to compare the size of adjacent elements in an array, but this does not compare the case of an array containing nil values;



arr.sort{|a,b| a.to_s <=> b.to_s}



Description: In this arr is an array object, A and B represent the adjacent two elements. This method uses the To_s method to convert nil to an empty string so that the array containing nil can be sorted. In addition, the value of this array itself does not occur any more.






6. Add and remove array elements:



To add an element to an array:



Unshift: Adding an array element to the head of an array



Push: Mantissa group element to array header



<<: Add an array element to the tail of the array



To delete an element to an array:



Shift: Deletes an array element from the head and returns the deleted element



POPs: Deletes an array element from the tail and returns the deleted element



Ruby Array Padding method
December 17 17:38 Author: zool



Manual


The code is as follows

Array.fill (obj) →array
Array.fill (obj, start [, length]) →array
Array.fill (obj, range) →array
Array.fill {|index| block}→array
Array.fill (start [, length]) {|index| block}→array
Array.fill (range) {|index| block}→array


The first three methods are populated with obj in array, start defaults to 0, length defaults to self.length length



The latter three methods are to populate the array with the return values in the block. The block receives the element index value of the array.



Instance Code



A = ["A", "B", "C", "D"]



A.fill ("x") #=> ["X", "X", "X", "X"]
#Replace all elements in the array with "X"

A.fill ("Z", 2, 2) # => ["X", "X", "Z", "Z"]
#From the array first [2] bit element begins, replaces 2 times "Z"

A.fill ("Y", 0..1) # => ["Y", "y", "z", "Z"]
Replace the element with "y" #The array [0] to [1] bit

A.fill {| i | i * i} # => [0, 1, 4, 9]
#Provide block method, I am an array subscript

A.fill (-2) {| i | i * i * i} # => ['A],' B ', 8, 27]
#From the array first [-2] bit starts running block
#Note, the annotations in Ruby's source code are wrong.
#a. Fill (-2) {| i | i * i * i} # => [0, 1, 8, 27]

#Actual application, I'm going to generate an array of numbers from 1 to 50.


The code is as follows

>> array.new. Fill {|i| I+=1}
=> [1, 2, 3.4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]


Perhaps the most powerful operation in the Ruby array can be found in mathematical operators-they can create new arrays from the array now. For example, the + operator allows you to create a new array from a two-array connection, and the * operator allows you to copy or connect an array itself several times. shirt_information= [14.5,



The code is as follows

"Long", 32, 49.99]
PANT_INFORMATION=[34,32,59.99]
Shirt_information + pant_information result is
[14.5, "Long", 32, 49.99, 34, 32, 59.99] The result is
[14.5, "Long", 49.99,14.5, "Long", 32, 49.99]


Result is


  code is as follows

/td>


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.