Hacker Monthly #21 by Netizens Media
Author:Netizens Media [Media, Netizens]
Language: eng
Format: epub, mobi, pdf
Published: 2012-02-02T16:04:10+00:00
You can see that the RString structure contains two values: ptr and len, but not the actual string data itself. Ruby actually saves the string character values themselves in some memory allocated from the heap, and then sets ptr to the location of that heap memory and len to the length of the string.
Hereâs a simplified version of the C RString structure:
struct RString {
long len;
char *ptr;
};
Iâve simplified this a lot; there are actually a number of other values saved in this C struct. Iâll discuss some of them next and others Iâll skip over for today. If youâre not familiar with C, you can think of struct (short for âstructureâ) as an object that contains a set of instance variables, except in C thereâs no object at all â struct is just a chunk of memory containing a few values.
I refer to this type of Ruby string as âHeap Stringâ since the actual string data is saved in the heap.
Shared Strings
Another type of string value that the Ruby interpreter uses is called a âShared Stringâ in the Ruby C source code. You create a Shared String every time you write a line of Ruby code that copies one string to another, similar to this:
str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
str2 = str
Here the Ruby interpreter has realized that you are assigning the same string value to two variables: str and str2. So in fact thereâs no need to create two copies of the string data itself. Instead, Ruby creates two RString values that share the single copy of the string data. The way this works is that both RString structs contain the same ptr value to the shared data⦠meaning both strings contain the same value. Thereâs also a shared value saved in the second RString struct that points to the first RString struct. There are some other details, which Iâm not showing here, such as some bit mask flags that Ruby uses to keep track of which RStrings are shared and which are not.
Download
Hacker Monthly #21 by Netizens Media.mobi
Hacker Monthly #21 by Netizens Media.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Influence: The Psychology of Persuasion by Robert B. Cialdini(4601)
The Miracle Morning by Hal Elrod(4422)
The Hacking of the American Mind by Robert H. Lustig(4085)
Pre-Suasion: A Revolutionary Way to Influence and Persuade by Robert Cialdini(3977)
Unlabel: Selling You Without Selling Out by Marc Ecko(3470)
Ogilvy on Advertising by David Ogilvy(3327)
Hidden Persuasion: 33 psychological influence techniques in advertising by Marc Andrews & Matthijs van Leeuwen & Rick van Baaren(3292)
Purple Cow by Seth Godin(3069)
Who Can You Trust? by Rachel Botsman(3024)
Kick Ass in College: Highest Rated "How to Study in College" Book | 77 Ninja Study Skills Tips and Career Strategies | Motivational for College Students: A Guerrilla Guide to College Success by Fox Gunnar(2998)
This Is Marketing by Seth Godin(2901)
I Live in the Future & Here's How It Works by Nick Bilton(2844)
The Marketing Plan Handbook: Develop Big-Picture Marketing Plans for Pennies on the Dollar by Robert W. Bly(2793)
The Power of Broke by Daymond John(2774)
Building a StoryBrand by Donald Miller(2754)
The 46 Rules of Genius: An Innovator's Guide to Creativity (Voices That Matter) by Marty Neumeier(2678)
Draw to Win: A Crash Course on How to Lead, Sell, and Innovate With Your Visual Mind by Dan Roam(2639)
The Tipping Point by Malcolm Gladwell(2558)
Market Wizards by Jack D. Schwager(2539)
