From: Daniel Brockman Date: 2005-07-02T11:41:22+09:00 Subject: Re: How and where Fixnum are created "Eust叩quio Rangel de Oliveira Jr." writes: > Hey Daniel. :-) > > | Later, when you say this, > | foo.moomin > | Ruby sees that `foo' is divisible by four and so must be a pointer > | to an object, and a regular method call is made. > > A regular method is called on the object at a memory location > allocated for the object value OR for value and a representation of > the object class, with methods and so on? > > What is pointed by, let's say, a variable who stores a reference to > a String like "this is a long string where I can write a lot of > things" is just the string value or a complete package with it's > value and methods? Basically just the string data and a pointer to the String class. > What will be released by the garbage collector is the answer of the > question above (memory allocated for values OR for values and > methods), when is not needed anymore, right? Just the memory for the data. The methods are still needed because there are other strings around. > For example on > > s1, s2, s3, s4 = "this","is","a","test" > > s1, s2, s3, s4 shares the String methods and their references points > to memory locations only with "this" "is" "a" "test", querying the > String methods on a String representation Well, a String object is not just a character array, struct RBasic { unsigned long flags; VALUE klass; }; struct RString { struct RBasic basic; long len; char *ptr; union { long capa; VALUE shared; } aux; }; but on the other hand, the list of method pointers is certainly not duplicated for each and every string out there. [Good call showing actual source code, Dominik.] > OR > > the reference to "this" & object methods, "is" & object methods, "a" > & object methods and "test" & object methods? No, this is not how it works. That would be terribly inefficient. [...] > I think it should be first one, specially when we change the String > class at runtime and it changes all the String references, but I'm > asking just to be sure. :-) You are correct. :-) > | bar.snufkin > | it sees that `bar' contains 85, which is an odd integer --- not > | divisible by four. The oddness means that `bar' is an immediate > | Fixnum, so Ruby converts the value to its real meaning by > | subtracting one and halving it, giving 42. Then the method > | `snufkin' is looked up in the Fixnum class and invoked with `self' > | bound to 42. > > That give us the value of the Fixnum, where we can find it's real > value, as you explained, and on this case the method (snufkin) is > looked up on a generic (or "internal") Fixnum class, like I supposed > String above, not on the reference (the reference is the own object) > as above right? Kind of > > n1, n2 = 1, 2 > > +-------+ +---------+ +-------+ > | value | | methods | | value | > +-------+ +---------+ +-------+ > n1 = | 1 +---| succ +---+ 2 | = n2 > +-------+ +---------+ +-------+ No, what happens is more like this: +---------+ n1 = 0x00000003 | Fixnum | +---------+ n2 = 0x00000005 | succ | +---------+ The `n1' and `n2' variables don't reference any _actual_ objects. Ruby pretends the objects are there so we can feel warm and fuzzy. It would be too inefficient to allocate a separate memory block for each and every small integer out there. > So we can make math right with it's value, as Robert said, not > needing to make an overhead with methods and so on, right? Robert > answered me about this but his email is on my mailbox on my job. :-( Right. Converting plain C integer to mangled Ruby fixnum (INT2FIX) or the other way around (FIX2INT) is really cheap. > | Okay, I realize that this explanation turned out pretty confusing. > | For more information, you might search for ``ruby immediate values.'' > > No, I really appreciated it. Thank you very much. Sorry for bore you > guys with this kind of thing but I'm really curious. :-) I'm glad I could be of help. If this subject truly bored me, I would probably not have responded to your question at all. The truth is that I needed to freshen up a bit on this myself. :-) > I searched for that on the web, but I really needed some more > "deeper" info, because there's a lot of places with just replicate > the pickaxe contents. :-) Ah, yes, that's annoying. Man pages, in particular, tend to be replicated across so many pages that they pollute search results. One copy is quite enough please. -- Daniel Brockman So really, we all have to ask ourselves: Am I waiting for RMS to do this? --TTN.