On Friday 06 June 2008 06:31:59 Dave Bass wrote:
>   def upper(x)
>     x.upcase!
>   end

It depends very much what your intent was -- upcase without the bang will 
return an upcase'd version, leaving the original untouched. A closer version 
to your Perl example might be:

def upper(x)
  x = x.upcase
end

The original string is untouched, but the reference 'x' now points to the 
result of that upcase.

Given that a lot of Ruby methods follow that convention, it feels like 
pass-by-value, and performs like pass-by-reference. (In theory; I guess we'll 
have to wait for 1.9 for real performance.)

> The original a is changed because upper has direct access to it.

Well, and because a method was called on it. A contrived, untested Perl 
example:

package String;
sub new {
  my $class = shift;
  my $self = {string => shift};
  bless $self => $class;
}

sub upcase {
  my $self = shift;
  $self->{string} =~ tr/a-z/A-Z/;
}

sub upper {
  my $x = shift;
  $x->upcase();
}

my $string = String->new('hello');
upper($string);

At this point, $string should have a member value of 'HELLO'.

In other words: Perl, like Ruby, represents objects as references. The only 
difference is, in Ruby, all values are objects. In Perl, there's a set of 
primitive values -- strings, numbers, arrays, and hashes -- which are not 
objects.

(Maybe they are with some deep voodoo like ties, but I never got that deep in 
Perl.)