The use of arrays and hash lists in Ruby _ruby topics

Source: Internet
Author: User

Ruby Arrays (arrays) and hash lists (hashes) are indexed collections (indexed collections).

Both store the collection of objects, accessed by key. The key of the array is an integer. The hash list supports any object as its key. Arrays and hashes are resized to save the new elements as needed. Accessing array elements is efficient, but the hash table provides flexibility. Any specific array or hash table can hold different types of objects.

Use array literal (array literal)--that is, to place a set of elements between square brackets--you can create and initialize new array objects. With an array object, a single element can be accessed by providing an index between the brackets, as shown in the following example, noting that the index of the Ruby array starts at zero.

A=[1, ' cat ', 3.14]     #有三个元素的数组

#访问第一个元素

a[0]  -> 1

#设置第三个元素

a[2] = nil

#显示这个数组

A- >[1, ' cat ', nil]

You may have noticed that the particular value of nil is used in this example. The concept of nil (or null) in many languages means "no objects". In Ruby, this is not the same; nil is an object, just like any other object, except that it is used to indicate an object without anything.

Sometimes it's painful to create an array of words--to handle many quotes and commas. Fortunately, Ruby has a quick way:%w can do what we want to do.

A = [' Ant ', ' bee ', ' cat ', ' dog ', ' elk ']

a[0]-> "ant" A[3

]-> "Dog"

#this is the same:

a =%w{ant to be E Cat Dog Elk}

a[0]-> "ant"

a[3]-> "Dog"

Ruby's hash table is similar to an array. The hash literal (hash literal) uses curly braces instead of square brackets. This literal must provide two objects for each item: a key and a value (value).

For example, you might want to map an instrument to the symphony chapter they belong to, and you can do it in a hash table:

Inst_section = {

' cello '   => ' string ',

' clarinet ' => ' woodwind ',

' drum ' => ' percussion '  ,

' oboe '   => ' woodwind ',

' trumpet ' => ' brass ', ' violin ' => '   string '

}

The left side of the => is the key (key) and its corresponding value (value) on the right. In a hash table, the key must be unique (no two "drum" items). The keys and values in the hash table can be any object-you might have such a hash list, which is the value of an array or other hash table.

The hash table is indexed using the same square brackets notation as the array.

inst_section[' oboe ']           -> "woodwind" inst_section[' cello '

]            -> "string"

inst_section[' Bassoon ']         -> Nil

As shown in the example above, the hash table returns nil by default if you index with a key that is not included in a hash table. This is usually convenient, such as nil in a conditional expression that means false. And sometimes you might want to change the default action. For example, if you use a hash table to calculate the number of times each key appears, it is convenient if the default value is 0. This is easy: when you create a new empty hash list, you can specify a default value.

Histogram = hash.new (0)

histogram[' Key1 ']            ->0 histogram['

key1 '] = histogram[' key1 ']+1

histogram [' Key1 ']            ->1

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.