Hi --

On Wed, 19 Jan 2005, Glenn Parker wrote:

> Florian Gross wrote:
>> 
>> See my earlier posting about the lambda { } and Variable[:name] ways of 
>> doing this.
>
> Sorry, but I can't believe anybody would actually want to use the lambda 
> styles as proposed.  They are ugly and obscure.
>
>> Ruby itself just treats variables as names for Objects. You can't really 
>> refer to a variable itself, you can only refer to the Object it is a name 
>> for. That's a surprisingly simple and non-confusing model.
>
> Judging by the frequency that this issue is discussed, it's more confusing 
> than you suggest.

Then again, I've found that when people get confused by things in
Ruby, it's very often because Ruby is simpler and less cluttered than
they expect it to be.  My favorite example of this is the singleton
class model.

> A typical programmers expects call-by-reference to work 
> one way, and Ruby works differently.  Extra confusion results because this 
> difference is masked by using self-updating methods, but it always fails for 
> immediate objects, and it eventually fails in a surprising way for 
> non-immediate objects.  I call it surprising because a typical programmer 
> does not expect the assignment operator to destroy call-by-reference, but 
> that is exactly what happens.
>
>  def inc1(a, i); a += i; end
>  def inc2(a, i); a = a + i; end
>
> To the naive Ruby programmer, inc1 and inc2 seem to be equivalent, but Ruby 
> gurus just shake their heads and sigh while they explain, yet again, that 
> there is a difference.  Why is this considered a good thing?  :)

We must know different Ruby gurus -- the ones I know are remarkably
gracious, even enthusiastic, about explaining things :-)

In any case, I believe the two things in your example are actually
equivalent.  My understanding is that Ruby always expands:

    x += y

to

    x = x + y

I don't think there are any cases where it makes any difference which
one you use, at least as far as I can remember.

As for confusion, typical programmers, etc.:  I don't think there's as
much of the "princess and the pea" syndrome among programmers as one
might think -- most people seem to be fairly adventurous and
thick-skinned -- and people certainly have to rise above that if they
want to explore more than a rather small number of programming
technologies.  Besides, languages with other models exist already.
Ruby also exists, and there's no more reason for Ruby to converge on
the model of some other language than for some other language to
converge on Ruby.  (Maybe even less :-)  Away with the Ruby
inferiority complex! :-)

> Ruby does *not* support call-by-reference, in the traditional sense of the 
> term.  Instead, it is strictly call-by-value, and formal paremeters are 
> copies of references from the calling scope.

This is the key to it, in my opinion.  Every "value" in Ruby is a
reference, and every reference to an object is the same distance from
the object as every other reference to that object (as in: a =
Object.new; b = a;  a and b are now equivalent with respect to
their distance from the object).


David

-- 
David A. Black
dblack / wobblini.net