Array usage in Ruby, ruby array usage

Source: Internet
Author: User

Array usage in Ruby, ruby array usage

Ruby arrays are ordered. What about any object ?? The set of integer indexes. The elements in each array are associated and an index is mentioned.

Array subscript starts from 0, such as C or Java. Negative index assumes that the end of the array --- that is,-1 indicates 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 accommodate 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. The Ruby array automatically grows and adds elements.
Create an array:

There are many ways to create or Initialize an array. One method is the new class method:

names = Array.new

You can set the size of an array. When creating an array:

names = Array.new(20)

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

#!/usr/bin/rubynames = Array.new(20)puts names.size # This returns 20puts names.length # This also returns 20

This produces the following results:

2020

You can specify each element in a value array as follows:

#!/usr/bin/rubynames = Array.new(4, "mac")puts "#{names}"

This produces the following results:

macmacmacmac

You can also use the new block to calculate and fill each element:

#!/usr/bin/rubynums = Array.new(10) { |e| e = e * 2 }puts "#{nums}"

This produces the following results:

024681012141618

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

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

Create an array in another form as follows:

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

The core of the Ruby kernel module has an array method that only accepts one parameter. Here, the number of an array is created as a parameter within the scope of the method:

#!/usr/bin/rubydigits = Array(0..9)puts "#{digits}"

This produces the following results:

0123456789

Built-in array methods:

We need an Array object to call an instance of the Array method. As we can see, the following is an Array object to create an instance:

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

This will return a new array to fill in the given object. Now, using the created object, we can call any available instance method. For example:

#!/usr/bin/rubydigits = Array(0..9)num = digits.at(6)puts "#{num}"

This produces the following results:

6

For example:

Try the following example to collect various data.

a = [ "a", "b", "c" ]n = [ 65, 66, 67 ]puts a.pack("A3A3A3")  #=> "a b c "puts a.pack("a3a3a3")  #=> "a\000\000b\000\000c\000\000"puts n.pack("ccc")   #=> "ABC"

This produces the following results:

a b cabcABC

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.