[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

pass by reference?

andy

1/26/2007 11:28:00 PM

15 Answers

Vincent Fourmond

1/26/2007 11:33:00 PM

0

Andy Koch wrote:
> Hello,
>
> Is there a way to pass variables by reference into function.
>
> I have large string to pass and the pass by value seems to be eating up
> too much memory.

You don't actually need to do anything: everything in ruby is passed
by reference, (excepted integers, symbols, nil, false and true - am I
missing one ?)

Cheers,

Vincent

--
Vincent Fourmond, PhD student (not for long anymore)
http://vincent.fourmon...

Per Velschow

1/26/2007 11:35:00 PM

0

Everything is pass-by-reference in Ruby. There is no pass-by-value. So
your memory problems must have another cause.

Andy Koch wrote:
> Hello,
>
> Is there a way to pass variables by reference into function.
>
> I have large string to pass and the pass by value seems to be eating up
> too much memory.
>
> Thanks,
>
> Andy


--
Posted via http://www.ruby-....

WoNáDo

1/26/2007 11:54:00 PM

0

Per Velschow schrieb:
> Everything is pass-by-reference in Ruby. There is no pass-by-value. So
> your memory problems must have another cause.

May be, that you do in fact a copy by some other operations, that create a new
String object and copy the original. E.g.

s = t + ''

creates a new string with the same contents. There are several possibilities to
do things like that without recognizing it.

Wolfgang Nádasi-Donner

dblack

1/27/2007 12:16:00 AM

0

Austin Ziegler

1/27/2007 6:30:00 PM

0

On 1/26/07, Vincent Fourmond <vincent.fourmond@9online.fr> wrote:
> Andy Koch wrote:
> > Hello,
> >
> > Is there a way to pass variables by reference into function.
> >
> > I have large string to pass and the pass by value seems to be eating up
> > too much memory.
>
> You don't actually need to do anything: everything in ruby is passed
> by reference, (excepted integers, symbols, nil, false and true - am I
> missing one ?)

Those are still passed by reference. It just so happens that they are
unique and immutable in the system, so it looks a lot like
pass-by-value.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Xavier Noria

1/27/2007 6:52:00 PM

0

On Jan 27, 2007, at 7:29 PM, Austin Ziegler wrote:

> On 1/26/07, Vincent Fourmond <vincent.fourmond@9online.fr> wrote:
>> Andy Koch wrote:
>> > Hello,
>> >
>> > Is there a way to pass variables by reference into function.
>> >
>> > I have large string to pass and the pass by value seems to be
>> eating up
>> > too much memory.
>>
>> You don't actually need to do anything: everything in ruby is
>> passed
>> by reference, (excepted integers, symbols, nil, false and true - am I
>> missing one ?)
>
> Those are still passed by reference. It just so happens that they are
> unique and immutable in the system, so it looks a lot like
> pass-by-value.

I think the problem in these discussions is that "reference" in the
programming language jargon is not the same word as in "pass-by-
reference". This discussion is common in Java forums as well. Java is
pass-by-value.

In C++ since the copy constructor is involved, I found people in
freenode#c++ consider &-arguments to be pass-by-reference because
they can modify the objects in the caller, albeit the assigment test
discussed in the thread does not work, that'd be

def m(b)
b = 7
end

a = 3
m(a)
# a expected to be 7 in pass-by-reference

-- fxn


Thomas Hafner

1/27/2007 7:16:00 PM

0

Xavier Noria <fxn@hashref.com> wrote/schrieb <73286BF0-AFE1-40C1-9704-16A3E129CD56@hashref.com>:

> In C++ since the copy constructor is involved, I found people in
> freenode#c++ consider &-arguments to be pass-by-reference because
> they can modify the objects in the caller, albeit the assigment test
> discussed in the thread does not work, that'd be
>
> def m(b)
> b = 7
> end
>
> a = 3
> m(a)
> # a expected to be 7 in pass-by-reference

That's another story. With the code above you rather reset b to a new
object instead of modifying the passed object. Look here:

def m(b)
b.shift
b.unshift(7)
end
a = [3]
m(a)
=> [7]

Would that be the same with pass-by-value? No, I don't think so.

Regards
Thomas

Xavier Noria

1/27/2007 7:41:00 PM

0

On Jan 27, 2007, at 8:25 PM, Thomas Hafner wrote:

> That's another story. With the code above you rather reset b to a new
> object instead of modifying the passed object. Look here:
>
> def m(b)
> b.shift
> b.unshift(7)
> end
> a = [3]
> m(a)
> => [7]
>
> Would that be the same with pass-by-value? No, I don't think so.

Yes, that's the usual point in the discussion. Being able to change
the internal state of mutable objects is not a test for pass-by-
reference. Java is pass-by-value for instance (according to the JLS)
and that test passes.

Problem with the assignment-seen-in-the-caller test is what dblack
pointed out, it mixes assigment semantics.

In Perl, which is pass by reference, you can do this for example:

$ perl -wle '$x = 0; sub { ++$_[0] }->($x); print $x'
1

-- fxn


Austin Ziegler

1/27/2007 7:51:00 PM

0

The simplest way to remember this is that variables in Ruby aren't
chunks of memory. When C++ programmers say that they're passing
something by reference, they are simply doing a (slightly) safer
operation than passing a pointer around. They are *still* referencing
the memory block, but it's type checked.

In Ruby, inasmuch as variables contain anything, they contain a
reference. This applies even to immediate values such as nil, true,
false, integers, and symbols. It just so happens that, at least for
integers, the reference (the object ID, if you will) is the same as
the value, shifted left once -- at least in matzruby. The others are
all fixed object IDs.

Making a distinction between the passing semantics of immediate values
is a mistake; it invites people to peer too deeply under the hood of
Ruby. Better to clarify that integers are immediate immutable values
and indicate that all variables are references and the references are
passed -- by value -- to methods.

Scope also enters into this, but that's been discussed clearly.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

dblack

1/28/2007 4:09:00 AM

0