Austin Ziegler
5/6/2005 5:15:00 PM
On 5/4/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:
[...]
>> What are you trying to do? There is no equivalent to what you
>> want in Ruby, but in most cases, it's not necessary. A little bit
>> of rethinking, on the other hand, is necessary.
> I've have various ways I can do what I need. I just wanted to
> see if I could do it the perl way.
I'm not sure why.
>> There is no way to do the following:
>>
>> a = [1, 2, 3]
>> x = a[1]
>> x = 4 # a == [1, 4, 3]
>>
>> Variables in Ruby are simply labels for object references. They
>> are not objects themselves. In Ruby, variables are transient
>> names -- they are effectively "weightless" and take up no space
>> (effectively).
> Ruby variables must at least take up the space of a pointer (32
> or 64 bits) and I would also imagine an entry in a symbol
> table.
Must they? I'm not sure of that. *Objects* in Ruby take up a certain
amount of space, but whether the various labels for those objects
take up any space at all is a different question -- and completely
irrelevant to the execution of Ruby.
> But, that space probably isn't tracked by ObjectSpace/GC. Not
> tracking that space like an object is probably the reason why you
> can't have a general reference to an object reference like perl
> has.
With solely an implementation perspective, you might be right.
However, Matz could have easily made that space accessible -- Perl
did it, after all -- and could have made it so that variables were
real objects. He chose not to. Variable names in Ruby are just
labels. How much space they take up is completely irrelevant.
Matz deliberately chose to stay away from Perl-like semantics here
because Perl references and C pointers are some of the largest
sources of errors in those languages.
>> In Perl and C-like languages, variables themselves take up space
>> on the call stack and may refer to other places on the heap.
> You are probably right about perl (treat variables like objects),
> but C is more like ruby in this respect. In C most variables are
> on the stack and thus transient. You can make a reference to one
> of those variables (i.e. a pointer to somewhere in the stack), but
> that space will become invalid once the variable goes out of
> scope.
No, C is not at all like Ruby in this respect. The transience of a C
variable is, to be quite honest, something of an irrelevancy. You
*can* take an address of it. If I declare:
char* foo;
Yes, foo is a pointer to char, but I can still take the address of
foo (getting the pointer to the pointer). This cannot be done in any
way with Ruby. This is an undesirable thing in Ruby.
> Instead of having a general one-size fits-all reference to an
> object reference, you could make a new class for each type of
> object reference within each class. Of course you'd want a
> common store/retrieve interface to all of these. For example:
Sure, you can create a Ref class. If you look on ruby-talk archives,
you'll see several implementations in the past three years. However,
such an object is useless. It can't act as the object for which it
holds the reference. It won't report as the object for which it
holds the reference.
...
> Now, I think putting this type of thing in the libraries could
> be quite useful. At a minimum, it would allow you to easily do
> the pass-by-reference thing that I've seen several questions
> on.
No, it wouldn't (to either question: be useful, or allow
pass-by-reference). Ruby's semantics are pass-reference-by-value.
This is something that people who use Ruby just have to get used to.
That you can do what you've done in this and the next post shows
that Ruby is powerful -- but neither is useful to most (I'd argue
99%+ development cases). I have had little need for such an object
at all. (There's one case that I have in PDF::Writer where such a
reference would be mildly useful, but the reality of what I need to
do here is more the elimination of all instance variable use in what
I'm trying to do. And *that* is a different beast entirely.)
-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca