Detailed explanation of string in Ruby language

Source: Internet
Author: User

The string in the Ruby language is mutable, unlike a string in Java, C #, which is immutable. Like what

str1= "abc"
str2= "abc"

In Java, for literal strings, a table is maintained inside the JVM, so if in Java, str1 and str2 are the same string objects. In Ruby, str1 and str2 are completely different objects. Similarly, the operation of a string object in Java produces a new object, while Ruby manipulates the same object, such as:

Str= "abc"
Str.concat ("CDF")

At this point str is "ABCCDF". What does Ruby do with string? We only talk about the implementation of C Ruby, interested in first look at this article "a glimpse of ruby--object base." In Ruby.h, we can see the structure of the string object, and the objects in Ruby (including the class as well as objects) are one of the struct,string and no exception:

struct Rstring {
struct rbasic basic;
Long Len;
char *ptr;
Union {
long CAPA;
VALUE shared;
} aux;
};
Ruby.h

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.