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