Josh Cheek wrote:
>> *But I can change the integer a variable holds by passing a pointer in C!*
>>
>> That is true, but you are not passing the integer, you are passing a
>> pointer to the integer. Since C is pass-by-value, if you had a variable
>> holding the pointer before the call, you can be totally certain the variable
>> will hold the same exact pointer after the call.
>>
> It seems that you are differentiating them based on whether the pointer 
> is
> passed implicitly or explicitly. In other words, in C, you know you 
> passed
> the pointer, in Perl, the language did it for you.

I think the difference is whether you are passing a reference to the 
variable, or to the object itself.

In this code:

var = "abc"
foo(var)

'var' holds a reference to a String object, and that reference is passed 
(by value) to foo. foo never knows that the object reference came from 
local variable 'var', nor does it have any way to change var (*).

In C, it is possible to take a pointer to the local variable itself (the 
memory slot which is the local variable), and hence to make remote 
changes to the variable.

  int a = 123;
  foo(&a);

  ...

  void foo(int *a) {
    (*a)++;
  }

In C++ you can have a 'reference' to the local variable, which avoids 
explicit dereferencing (and adds enormously to confusion).

In Perl you can have references to variables too:

my $a = 123;
foo(\$a);
print "$a\n";
exit 0;

sub foo {
  my $vref = shift;
  $$vref++;
}

But in ruby, you can't get a reference to a local variable or an 
instance variable or anything else which holds an object reference.

If you are a C user, it may be easiest to think that:
* every Ruby value is a pointer [to an object]
* you cannot obtain a pointer to a local variable or an instance 
variable. You can only copy the pointer value itself.

Fixnum/nil/false/true are special cases. Rather than creating an object 
representing each value, and taking a pointer to that object, the value 
is buried within the pointer itself.

Regards,

Brian.

(*) Unless you pass a Binding object explicitly, in which case it is 
possible to eval code which affects local variables in other scopes.
-- 
Posted via http://www.ruby-forum.com/.