Ruby Study Notes (11)-symbol and hash Parameters

Source: Internet
Author: User

I won't go into any further discussions about symbol. I just want to talk about the advantages of it.

Each object in ruby is identified and distinguished by a digital ID. You can use XXX. object_id to view the ID.

 
Puts "0001". object_idputs "0001". object_idputs "0001". object_idputs "0001". object_id

The output result is similar to the following:

32088750
32088730
32088710
32088690

It can be found that even strings with the same content "0001" are processed as different objects each time in the Ruby interpreter, so as the number of calls in the characters increases, isn't the burden on Ruby interpreters getting heavier and heavier, and memory consumption getting bigger and bigger?

Is there a way for Ruby to record it internally? If a string with the same content is encountered, it does not need to generate a new String object, but directly retrieve an existing object and return it? The answer is symbol.

If you add a colon (":") to the front of an object, you can get the corresponding symbol.CodeChanged:

 
Puts: "0001". object_idputs: "0001". object_idputs: "0001". object_idputs: "0001". object_id

The output result is similar to the following:
155038
155038
155038
155038

By comparing the output just now, we will find that this call seems to be four times, and it seems that all of them share one object.

Now you can understand why ror applications are filled with calls similar to the following:

Link_to 'show ',: Action => 'show',: Id => product

Here, the purpose of using: action rather than action; using: Id rather than ID is the benefit of the symbol mentioned above.

What does "=>" mean, that is, the hash parameter to be discussed below:

Let's take a look at the hash table (hashtable), which is basically a set of "key-value" pairs.

 
Products = {"0001" => "Mobile Phone", "0002" => "computer"} puts products ["0001"]

Output result:
Mobile phone

It's easy to understand, isn't it? However, based on the content mentioned above, if puts products ["0001"] is used multiple times in the future, Ruby will generate a brand new "0001" String object each time, so it is generally not used in this way, this should be changed:

 
Products = {: "0001" => "Mobile Phone",: "0002" => "computer"} puts products [: "0001"]

A hash parameter is used to pass in a hash table as a parameter when calling a method and passing in parameters.

 
Def my_method (P1, P2, options ={}) puts P1 puts P2 options. each {| key, value | puts "# {key} is # {value}"} endmy_method ("1", "2",: Title => "title ",: id = & gt; 123)

In the Parameter definition, we use options ={} to define an empty hash set. In this way, we can input any xx => YY, AA => BB, 11 ==> 22... is the parameter flexible?

Output result:

1
2
Title is Title
ID is 123

Let's write more "Tide" and Ruby:

 
My_method "1", "2",: Title => "heading",: Id => 123

The results are exactly the same. Is this statement similar to link_to 'show ',: Action => 'show',: Id => product?

Digression: the hash parameter and the optional parameter are still somewhat different (for optional parameters, see "Ruby Study Notes (3)-syntactic foresight"). I personally think that the hash parameter is actually more flexible.

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.