Ruby string frozen and unfreeze Problems

Source: Internet
Author: User
Ruby string forzen and unfreeze problem string processing is the first problem question to learn a new language

 

When we look at the return of super people, remember there's a superman named freeze in it? This guy has a trick, drink water, and then blow everything out and then freeze. This is not Ruby, and freeze exists.

The code I wrote today contains the following line:
Strtmp = SD [0]. to_s.gsub! (/.../, ''), SD is a hash

An error occurred while executing: 'gsub! ': Can't modify frozen string (typeerror)
Now that forzen is running, I assume that the code is changed:

Sdstr = SD [0]. to_s
Strtmp = sdstr. to_s.gsub! (/..../,'')
Same error: 'gsub! ': Can't modify frozen string (typeerror)

Think of what Meng Yan wrote today: the study of symbol of Ruby, indeed, there are many characteristics of Ruby in string processing.

 


3. Why can I save memory? String in ruby is a mutable object, which is different from Java, C #, and python. Note that it is different from the basic_string <t> of cow in some C ++ standard libraries. Every string in Ruby can be changed locally. This may be the reason why the two strings with the same content in ruby are actually two different objects.

A = "hello"
B = "hello"

Although both strings have the same content, you know a. object_id when comparing a and B! = B. object_id, which points to not the same object. The result is similar to the C language without string pooling optimization. Whether immutable is good or mutable is good, or whether it looks like a Smart cow is good. However, Ruby is designed to use character strings as hash keys. For example, if you write:

H ["Ruby"]. Name = "Ruby"
H ["Ruby"]. Author = "Matz"
H ["Ruby"]. birth_year = 1995

The "Ruby" string is dynamically generated three times, consuming three times of memory. This seriously wastes memory. Ruby is used as the key, because during the entire running process, Ruby runtime ensures that there is only one symbol object named Ruby, so there is no need to generate three, saving memory.

When reading the reference manual, we found that Ruby's string has the freeze method, but unfreeze is not provided. Finally, I had to honestly read the reference manual:

Sdurl = SD [0]. to_s.dup solves the frozen problem.

But Matz said it may be available in the future:

Uh, maybe. Some part of string copy-on-write system depends on
Fact that frozen strings wowould never be modified. I'm not sure proper
Copying on those two functions make unfreeze possible. When I confirm
It, I wowould happily add object # unfreeze to the future Ruby.
Matz.


In Ruby, the freeze problem occurs not only in strings, but also in array and hash frozen. For more information, see the following article:

Ruby array and hash frozen Behavior

Ruby has been in touch with Ruby for a while, and it makes me very happy. Ruby is more natural than python.


I used to analyze some of the experiences of the Java Nanning (Nanning) AOP module, coupled with the current feeling of using Ruby, and found that Ruby as a domain language does have many advantages.
A taste of edevil. RB: using DL to unfreeze Objects gives me more inspiration.

Related Links

Ruby string forzen and unfreeze Problems
Ruby string forzen and unfreeze Problems

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.