Introduction to array and Hash in Ruby concise tutorial

Source: Internet
Author: User

Today we are studying arrays and Hash. arrays are often used in many programming languages.

Array:

Arrays are a container for storing data. In Ruby, the data stored in arrays can be of any type. This is different from JAVA, an array is a structure that stores the same type of data.

1. How to define arrays in Ruby?

Use [] to enclose elements, while use "," to separate elements. As follows,

Copy codeThe Code is as follows:
Name = ["Windy", "Cindy", "Amee", "Tom"]

Name = [] # declares an empty array, an undefined Array

Name = [1, "song", 3, "Kobayashi"]

2. How to obtain array elements?

In Ruby, array elements are obtained through the array name [index], which is the same as in JAVA. The index number starts from 0. For example, in the above example, to retrieve the "Windy" element, you must use name [0], and p name [3] #-> "Tom ".

3. How to put objects in an array?

You can assign values to array elements directly.

Copy codeThe Code is as follows:
Name = ["Alice", "John", "Bob", "Jessie"]
Name [1] = "Jack"

P name #-> ["Alice", "Jack", "Bob", "Jessie"]

Name [4] = "Windy"
P name #-> ["Alice", "Jack", "Bob", "Jessie", "Windy"]

4. How to obtain the array size?

You can use the size method to return the array size. For example, in JAVA, the array name, p name. size #-> 4 defined for the first time in the above instance, the array has the attribute length to get the array size.

5. How to process the content in the array one by one?

You can use the each method of the array to process the content in the array one by one. The syntax is as follows:

Copy codeThe Code is as follows:
= Begin
Syntax:
Array. each {| variable |
Action to be executed
}
= End

Name = [2, 5, 6, 9]
Name. each {| number |
Prints number + 1, "\ n"
}

#-> 3
#6
#7
#10

This is similar to the for each loop in JAVA.

HASH:

HASH is one of the frequently used containers. In Ruby, it can use string and other data as keys to store objects, which is similar to Map containers in JAVA.

1. How to define HASH?

Use {} to box the HASH object, and each element appears in the form of key => value, and the elements are separated.

Copy codeThe Code is as follows:
Name_list = {"01" => "Windy", "02" => "Cindy", "03" => "Terry "}

2. How to obtain elements in HASH?

Use the HASH name [key value] to obtain the HASH element, for example,

Copy codeThe Code is as follows:
P name_list ["01"] #-> "Windy"

3. How to store objects in HASH?

Similar to the array mentioned above, the value is directly assigned to the HASH element, for example, name_list ["02"] = "Amy"

Because HASH does not have a fixed order, data cannot be retrieved in the storage order, which is similar to the Hash Table in JAVA.

4. How to process HASH content one by one

Similar to arrays, it also uses its each method to process its elements one by one.

Copy codeThe Code is as follows:
= Begin
Syntax
HASH. each {| key variable, value variable |
Action to be executed
}
= End

Name_list.each {| key, value |
Print key, "=", value, "\ n"
}

#-> "01" = "Windy"
# "02" = "Cindy"
# "03" = "Terry"

Ruby also has the redirect function, which is to store the execution result file to another file and use "> File Name". For example, ruby name_list.rb> name_list.txt

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.