Some related methods of using arrays in Ruby

Source: Internet
Author: User
Tags arrays pack

This article mainly introduces some of the relevant uses of the array in Ruby, is the basic knowledge of Ruby learning, need friends can refer to the

Ruby arrays are ordered, any object's?? A collection of integer indices. The elements in each array are associated with an index mentioned.

The array subscript starts at 0, such as C or Java. The negative index assumes the end of the array---that is,-1 represents the array index of the last element,-2 is the next element of the last element in the array, and so on.

Ruby arrays can hold objects such as strings, integers, long integers, hashes, symbols, and even other array objects. Ruby arrays are not as strict as arrays in other languages. Ruby arrays grow automatically while adding elements.

To create an array:

There are a number of ways to create or initialize an array. One way is the new class method:

?

1 names = Array.new

You can set the size of an array when the array is created:

?

1 Names = Array.new (20)

The array names now has a size or length of 20 elements. You can return a method of the size or length of an array:

?

1 2 3 4 5 #!/usr/bin/ruby names = Array.new puts Names.size # This returns puts # this names.length also 20

This will produce the following results:

?

1 2 20 20

You can specify each element in an array of values as follows:

?

1 2 3 4 5 #!/usr/bin/ruby names = array.new (4, "Mac") puts "#{names}"

This will produce the following results:

?

1 Macmacmacmac

You can also use the new block to compute the padding for each element:

?

1 2 3 4 5 #!/usr/bin/ruby nums = Array.new (Ten) {|e| e = e * 2} puts "#{nums}"

This will produce the following results:

?

1 024681012141618

There is another method, array, []. It works like this:

?

1 Nums = Array. [] (1, 2, 3, 4,5)

An array of another form is created as follows:

?

1 Nums = Array[1, 2, 3, 4,5]

Kernel module Ruby's core, there is an array of methods that accept only one parameter. Here the method scope creates an array of numbers as arguments:

?

1 2 3 4 5 #!/usr/bin/ruby digits = Array (0..9) puts "#{digits}"

This will produce the following results:

?

1 0123456789

Array-built methods:

We need an array object to invoke an instance of the arrays method. As we can see, here is how the array object is created to create an instance:

?

1 Array. [](...) [or] Array[...] [or] [...]

This returns a new array to populate the given object. Now using the created object, we can call any available instance methods. For example:

?

1 2 3 4 5 6 7 #!/usr/bin/ruby digits = Array (0..9) num = digits.at (6) puts "#{num}"

This will produce the following results:

?

1 6

For example:

Try the following examples to collect all kinds of data.

?

1 2 3 4 5 A = ["A", "B", "C"] n = [puts] A.pack ("a3a3a3") #=> "a b C" puts A.pack ("a3a3a3") #=> "A0000B0000C00" "Puts N.pack (" CCC ") #=>" ABC "

This will produce the following results:

?

a b C ABC abc
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.