Some related uses of arrays in Ruby _ruby topics

Source: Internet
Author: User
Tags arrays pack

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:

names = Array.new

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

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:

#!/usr/bin/ruby

names = Array.new
puts Names.size # This returns-puts Names.length
# This also returns 20

This will produce the following results:


20

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

#!/usr/bin/ruby

names = array.new (4, "Mac")

puts "#{names}"

This will produce the following results:

Macmacmacmac

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

#!/usr/bin/ruby

nums = array.new (Ten) {|e| e = e * 2}

puts "#{nums}"

This will produce the following results:

024681012141618

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

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

An array of another form is created as follows:

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:

#!/usr/bin/ruby

digits = Array (0..9)

puts "#{digits}"

This will produce the following results:

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:

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:

#!/usr/bin/ruby

digits = Array (0..9)

num = digits.at (6)

puts "#{num}"

This will produce the following results:

6

For example:

Try the following examples to collect all kinds of data.

A = ["A", "B", "C"]
n = [
puts] a.pack ("a3a3a3")  #=> "a b C"
puts A.pack ("A3A3A3") 
   #=> "a\000\000b\000\000c\000\000"
puts N.pack ("CCC")   #=> "ABC"

This will produce the following results:

A b c
ABC
ABC

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.