Ruby journey (11) arrays and hashes IN RUBY

Source: Internet
Author: User
Arrays in ruby are created by square brackets. The initial values can be placed in square brackets and separated by commas. array elements can be of different types and support addition and multiplication.

Ary = [1, 2, "3"]
Ary * 2 Returns [1, 2, "3", 1, 2, "3"]
Ary + ["Lee", "ho"] returns [1, 2, "3", "Lee", "ho"]
Ary [] returns [1, 2] and obtains two elements starting from position 0.
Ary [0 .. 2] returns [1, 2, "3"] From position 0 to position 2

Array support <operation means inserting an element at the end

Arrays and strings are converted to each other through join and split.
Ary. Join (",") returns "1, 2, 3"
"1, 2, 3". Split (",") Returns ["1", "2", "3"]

The visible array is very flexible.

Hash is also very flexible to use {} Structure
Hash = {"title" => "Hallo", 2 => "this is a hash "}
Hash ["title"] returns "Hallo"
A = "title"
Hash [a] returns Hallo
Hash [3] = "sample" adds an element whose key is 3 and value is "sample"
Hash. delete 3 Delete elements whose key is 3

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.