Pass by reference?

On Jan 27, 5:34 am, Wolfgang Nádasi-Donner [email protected]
wrote:

Code >>>>>

def modifying(a_param)
a_param = “hi”
end

def modifying2(a_param=a_value)
a_param = “hi”
end

In these two methods, the = sign is an assignment, which changes the
memory location of the lvalue to point to the object of the rvalue.

def modifying3(a_param)
a_param[0…-1]= “hi”
end

This is actually a method call. It could also be written a_param.
[]=(0…-1, “hi”) . So the difference is that in this example you are
actually calling a method on the object that a_param points to. This
is due to the syntactic sugar Ruby gives to the method named []= which
in this case is defined on String. See Ruby-Doc.org: Documenting the Ruby Language
ruby-doc-bundle/Manual/man-1.4/syntax.html#assign

So in all three we are passing by value as you define it.

[email protected] írta:

“When the method or constructor is invoked (§15.12), the values of the
actual argument expressions initialize newly created parameter variables,
each of the declared Type, before execution of the body of the method or
constructor. The Identifier that appears in the DeclaratorId may be used
as a simple name in the body of the method or constructor to refer to the
formal parameter.”

Thanks a lot, I was curious about this official description for a while!

What I miss, however, is the explicit use of the “pass by value”
expression. Bruce Eckel mentions in Thinking in Java, that both sides of
the controversy have lots of propagators. Both the “everything is pass
by value” theory, and the contrary one that he descrbes as:

“2. Java passes primitives by value (no argument there), but objects
are passed by reference. This is the world view that the reference is
an alias for the object, so you don’t think about passing references,
but instead say “I’m passing the object.” Since you don’t get a local
copy of the object when you pass it into a method, objects are
clearly not passed by value. There appears to be some support for
this view within Sun, since at one time, one of the “reserved but not
implemented” keywords was byvalue (This will probably never be
implemented).”

What is suprising is the reference (:-)) to Sun in this context, if the
JLS says otherwise.

Anyways, this is not a Java list, so sorry for beeing a bit offtopic –
I just hope it helps to show, there’s a great mess of notions around
this topic everywhere.

Bye:
Szab

How about:

class Vault
attr_accessor :unvault
def initialize val
@unvault = val
end
end

class Object
def to_vault
Vault.new(self)
end
end

def change_me(obj)
obj.unvault += " rocks!"
end

o = “Ruby”.to_vault
change_me(o)
p o.unvault

$ ruby vault-test.rb
“Ruby rocks!”

On 1/28/07, Thomas H. [email protected] wrote:

“Austin Z.” [email protected] wrote/schrieb [email protected]:

The simplest way to remember this is that variables in Ruby aren’t
chunks of memory.
Or: they are a chunk of memory, but the only kind of value they can
contain is a reference to an object. A variable is not such an object,
i.e. there are no references to variables.

It’s better not to consider variables in Ruby as anything but a label
and as such not a chunk of memory. Chunks of memory suggest object
status or possibilities; this is not part of Ruby.

Your graphs are pretty close to correct. I have to finish some
articles talking about variables in Ruby and post them at some point.

-austin

On 1/28/07, Grant H. [email protected] wrote:

end

In these two methods, the = sign is an assignment, which changes the
memory location of the lvalue to point to the object of the rvalue.

When talking about Ruby variables, it is ALWAYS incorrect to refer to
a “memory location.” It’s not a concept that has any useful meaning in
Ruby. Ever.

-austin

On 1/28/07, Tim X [email protected] wrote:

So, how successfully has that been at muddying the water? Everyone now nicely
confused?

Your description seems to be okay, but forget “address” when dealing
with Ruby variables. It’s not an appropriate concept and only muddies
the waters unnecessarily.

-austin