pass by reference?

A

Andy Koch

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
 
V

Vincent Fourmond

Andy said:
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
 
P

Per Velschow

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

Wolfgang Nádasi-Donner

Per said:
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
 
D

dblack

Hi --

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

I've always looked at it as: pass by value, where the "values" are
(mostly) references. For example:

a = "abcde" # a now contains a reference to that string
do_something(a) # pass the reference contained in a


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
P

Phrogz

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 ?)

How do you know if those are passed by reference or by value? It's a
very existential question:
If you pass an immutable type by reference, does it make a sound?
Er, I mean...
If you passed an immutable type by reference, how would you know that
it wasn't passed by value?

I leave you with this:

irb(main):001:0> values = [ 1, :foo, true, false, nil ]
=> [1, :foo, true, false, nil]

irb(main):002:0> puts values.map{ |v| v.object_id }
3
231138
2
0
4
=> nil

irb(main):003:0> def foo( *args ); puts args.map{ |v| v.object_id };
end
=> nil

irb(main):004:0> foo( *values )
3
231138
2
0
4
=> nil

irb(main):005:0> b = :foo
=> :foo

irb(main):006:0> b.object_id
=> 231138

It's almost the reverse of what you said - it's not that immediate
values are passed by value, but rather that every reference to an
immediate value refers to the same object.
 
M

Martin C. Martin

Phrogz said:
If you pass an immutable type by reference, does it make a sound?
Er, I mean...
If you passed an immutable type by reference, how would you know that
it wasn't passed by value?

Is there any way for the function you're calling to modify the value of
the variable in the caller? Pass by reference can do that.

- Martin
 
D

dblack

Hi --

Is there any way for the function you're calling to modify the value of the
variable in the caller? Pass by reference can do that.

You can modify the object to which the variable refers:

def change_me(obj)
obj << "hi"
end

arr = [1,2,3]
change_me(arr)
p arr # [1, 2, 3, "hi"]

In this example, arr contains a reference to an array. In change_me,
obj contains another copy of the same reference, so you can use it to
manipulate and change the original array.

I still wouldn't call this pass by reference (see my earlier post in
this thread).


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
R

Robert Klemme

Hi --

Is there any way for the function you're calling to modify the value
of the variable in the caller? Pass by reference can do that.

You can modify the object to which the variable refers:

def change_me(obj)
obj << "hi"
end

arr = [1,2,3]
change_me(arr)
p arr # [1, 2, 3, "hi"]

In this example, arr contains a reference to an array. In change_me,
obj contains another copy of the same reference, so you can use it to
manipulate and change the original array.

I still wouldn't call this pass by reference (see my earlier post in
this thread).

Absolutely right: Ruby uses pass by value - with references.

irb(main):004:0> def foo(x) x = 10 end
=> nil
irb(main):005:0> def bar; x = 20; foo(x); x end
=> nil
irb(main):006:0> bar
=> 20
irb(main):007:0>

There is no standard way (i.e. other than involving eval and
metaprogramming magic) to make a variable in a calling scope point to
another object. And, btw, this is independent of the object that the
variable refers to. Immediate objects in Ruby seamlessly integrate with
the rest (different like POD's in Java for example) and from a Ruby
language perspective you don't see any difference (other than
performance maybe). This is one of the reasons why Ruby is so elegant.

Kind regards

robert
 
D

dblack

Hi --

Hi --

Phrogz wrote:
If you pass an immutable type by reference, does it make a sound?
Er, I mean...
If you passed an immutable type by reference, how would you know that
it wasn't passed by value?

Is there any way for the function you're calling to modify the value of the
variable in the caller? Pass by reference can do that.

You can modify the object to which the variable refers:

def change_me(obj)
obj << "hi"
end

arr = [1,2,3]
change_me(arr)
p arr # [1, 2, 3, "hi"]

In this example, arr contains a reference to an array. In change_me,
obj contains another copy of the same reference, so you can use it to
manipulate and change the original array.

I still wouldn't call this pass by reference (see my earlier post in
this thread).


Just to demonstrate your point

def modifying a_param
a_param = "hi"
end

a_value = "low"

modifying a_value
case a_value
when "hi"
puts "Passed by reference"
when "low"
puts "Passed by value"
else
puts "Passed away"
end

That's a somewhat different point, though, having to do with variable
scope and (re)assignment semantics. What you're doing here
essentially is:

a = "low"
b = a
b = "hi"
puts a # low

That's going to happen whether a method call is involved or not.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
D

dblack

Hi --

Hi --

Phrogz wrote:
If you pass an immutable type by reference, does it make a sound?
Er, I mean...
If you passed an immutable type by reference, how would you know that
it wasn't passed by value?

Is there any way for the function you're calling to modify the value of
the variable in the caller? Pass by reference can do that.

You can modify the object to which the variable refers:

def change_me(obj)
obj << "hi"
end

arr = [1,2,3]
change_me(arr)
p arr # [1, 2, 3, "hi"]

In this example, arr contains a reference to an array. In change_me,
obj contains another copy of the same reference, so you can use it to
manipulate and change the original array.

I still wouldn't call this pass by reference (see my earlier post in
this thread).

Absolutely right: Ruby uses pass by value - with references.

irb(main):004:0> def foo(x) x = 10 end
=> nil
irb(main):005:0> def bar; x = 20; foo(x); x end
=> nil
irb(main):006:0> bar
=> 20
irb(main):007:0>

I'm not sure that demonstrates the "values that are references" thing,
though. You're reassigning to x in foo, which creates a new local x;
but I think that's just part of the assignment semantics. Or are you
assuming that if pass by reference were involved, then assignment
would work differently?


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
W

Wolfgang Nádasi-Donner

Code >>>>>

def modifying(a_param)
a_param = "hi"
end

def modifying2(a_param=a_value)
a_param = "hi"
end

def modifying3(a_param)
a_param[0..-1]= "hi"
end

a_value = "low"
modifying a_value
case a_value
when "hi"
puts "Passed by reference"
when "low"
puts "Passed by value"
else
puts "Passed away"
end

a_value = "low"
modifying2 a_value
case a_value
when "hi"
puts "Passed by reference"
when "low"
puts "Passed by value"
else
puts "Passed away"
end

a_value = "low"
modifying3 a_value
case a_value
when "hi"
puts "Passed by reference"
when "low"
puts "Passed by value"
else
puts "Passed away"
end

Passed by value
Passed by value
Passed by reference

I would name it somehow "call by assignment of value to local variable", because
the value of the actual parameter is assigned to the local variable used as a
formal parameter.

except for "Fixnum" etc. this is a pointer to an object. I one changes the
object itself, it will work like "by reference", otherwise only the local
variable is affected, without having any effect to the outside world.

Wolfgang Nádasi-Donner
 
W

Wolfgang Nádasi-Donner

Code >>>>>

def modifying(a_param)
a_param = "hi"
end

def modifying2(a_param=a_value)
a_param = "hi"
end

def modifying3(a_param)
a_param[0..-1]= "hi"
end

a_value = "low"
modifying a_value
case a_value
when "hi"
puts "Passed by reference"
when "low"
puts "Passed by value"
else
puts "Passed away"
end

a_value = "low"
modifying2 a_value
case a_value
when "hi"
puts "Passed by reference"
when "low"
puts "Passed by value"
else
puts "Passed away"
end

a_value = "low"
modifying3 a_value
case a_value
when "hi"
puts "Passed by reference"
when "low"
puts "Passed by value"
else
puts "Passed away"
end

Passed by value
Passed by value
Passed by reference

If would name it somehow "call by assignment of value to local variable", because
the value of the actual parameter is assigned to the local variable used as a
formal parameter.

except for "Fixnum" etc. this is a pointer to an object. I one changes the
object itself, it will work like "by reference", otherwise only the local
variable is affected, without having any effect to the outside world.

Wolfgang Nádasi-Donner
 
W

Wolfgang Nádasi-Donner

Code >>>>>

def modifying(a_param)
a_param = "hi"
end

def modifying2(a_param=a_value)
a_param = "hi"
end

def modifying3(a_param)
a_param[0..-1]= "hi"
end

a_value = "low"
modifying a_value
case a_value
when "hi"
puts "Passed by reference"
when "low"
puts "Passed by value"
else
puts "Passed away"
end

a_value = "low"
modifying2 a_value
case a_value
when "hi"
puts "Passed by reference"
when "low"
puts "Passed by value"
else
puts "Passed away"
end

a_value = "low"
modifying3 a_value
case a_value
when "hi"
puts "Passed by reference"
when "low"
puts "Passed by value"
else
puts "Passed away"
end

Passed by value
Passed by value
Passed by reference

I would name it somehow "call by assignment of value to local variable", because
the value of the actual parameter is assigned to the local variable used as a
formal parameter.

except for "Fixnum" etc. this is a pointer to an object. If one changes the
object itself, it will work like "by reference", otherwise only the local
variable is affected, without having any effect to the outside world.

Wolfgang Nádasi-Donner
 
D

dblack

---2049402039-1339491571-1169898401=:16717
Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-1339491571-1169898401=:16717"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

---2049402039-1339491571-1169898401=:16717
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

def modifying(a_param)
a_param =3D "hi"
end

def modifying2(a_param=3Da_value)
a_param =3D "hi"
end

def modifying3(a_param)
a_param[0..-1]=3D "hi"
end

a_value =3D "low"
modifying a_value
case a_value
when "hi"
puts "Passed by reference"
when "low"
puts "Passed by value"
else
puts "Passed away"
end

a_value =3D "low"
modifying2 a_value
case a_value
when "hi"
puts "Passed by reference"
when "low"
puts "Passed by value"
else
puts "Passed away"
end

a_value =3D "low"
modifying3 a_value
case a_value
when "hi"
puts "Passed by reference"
when "low"
puts "Passed by value"
else
puts "Passed away"
end

Passed by value
Passed by value

But you've broken the bindings. It's a whole new ballgame, as we say
here :)
Passed by reference

I would say: a reference, passed by value.
I would name it somehow "call by assignment of value to local variable",= =20
because the value of the actual parameter is assigned to the local variab= le=20
used as a formal parameter.

except for "Fixnum" etc. this is a pointer to an object. I one changes th= e=20
object itself, it will work like "by reference", otherwise only the loca= l=20
variable is affected, without having any effect to the outside world.

I consider the variable assignment semantics to be a separate matter.
For example:

def x(a)
a =3D "hello"
end

Here, Ruby's rule is that the second a is a completely new binding,
and destroys the original binding. I don't think that has anything to
do with whether the first a is a reference or a value. In other
words, even if Ruby had pass by reference, I assume that a =3D "hello"
would still destroy the binding and create a new local variable.

(Of course, it's purely speculative, since it would be a completely
different language.)


David

--=20
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
---2049402039-1339491571-1169898401=:16717--
---2049402039-1339491571-1169898401=:16717--
 
R

Robert Klemme

Hi --

Hi --

On Sat, 27 Jan 2007, Martin C. Martin wrote:

Phrogz wrote:
If you pass an immutable type by reference, does it make a sound?
Er, I mean...
If you passed an immutable type by reference, how would you know that
it wasn't passed by value?

Is there any way for the function you're calling to modify the value
of the variable in the caller? Pass by reference can do that.

You can modify the object to which the variable refers:

def change_me(obj)
obj << "hi"
end

arr = [1,2,3]
change_me(arr)
p arr # [1, 2, 3, "hi"]

In this example, arr contains a reference to an array. In change_me,
obj contains another copy of the same reference, so you can use it to
manipulate and change the original array.

I still wouldn't call this pass by reference (see my earlier post in
this thread).

Absolutely right: Ruby uses pass by value - with references.

irb(main):004:0> def foo(x) x = 10 end
=> nil
irb(main):005:0> def bar; x = 20; foo(x); x end
=> nil
irb(main):006:0> bar
=> 20
irb(main):007:0>

I'm not sure that demonstrates the "values that are references" thing,
though. You're reassigning to x in foo, which creates a new local x;
but I think that's just part of the assignment semantics. Or are you
assuming that if pass by reference were involved, then assignment
would work differently?

I probably should have chosen different variable names. I was trying to
make the point, that you cannot change a variable in bar by assigning in
foo.

Kind regards

robert
 
W

Wolfgang Nádasi-Donner

May be it will be clearer to name an assignment "a = someobject" by what it
really means: "Give a reference to object 'someobject' a name 'a' by
definition". It's close to the ":=" used for definition in mathematics.

If one uses this "a" as a "reference to 'someobject'" as an actual parameter in
a method call, it means, that the "reference to 'someobject'" is actually used
in the method.

Clearly you cannot change "a", because you don't have access to this name "a"
inside the method, but you can change the object "someobject" usually without
any problems. If you change "someobject" this will have consequences for 'a',
because the object ist changed, the reference is still the same.

Wolfgang Nádasi-Donner
 
D

dblack

---2049402039-392640456-1169901462=:17340
Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-392640456-1169901462=:17340"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

---2049402039-392640456-1169901462=:17340
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

May be it will be clearer to name an assignment "a =3D someobject" by wha= t it=20
really means: "Give a reference to object 'someobject' a name 'a' by=20
definition". It's close to the ":=3D" used for definition in mathematics.

If one uses this "a" as a "reference to 'someobject'" as an actual parame= ter=20
in a method call, it means, that the "reference to 'someobject'" is actua= lly=20
used in the method.

Clearly you cannot change "a", because you don't have access to this name= "a"=20
inside the method, but you can change the object "someobject" usually wit= hout=20
any problems. If you change "someobject" this will have consequences for = 'a',=20
because the object ist changed, the reference is still the same.

Right -- I take that as the starting point -- but what I mean is that
the way the assignment/rebinding semantics work doesn't necessarily
pertain to the reference/value question.


David

--=20
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
---2049402039-392640456-1169901462=:17340--
---2049402039-392640456-1169901462=:17340--
 
W

Wolfgang Nádasi-Donner

Right -- I take that as the starting point -- but what I mean is that
the way the assignment/rebinding semantics work doesn't necessarily
pertain to the reference/value question.

I agree. reference/value came from the world before talking about objects. I
remember in Algol60 somehow the wording "after 'integer i;' the variable 'i IS
an integer".

We would never say that after writing "s = 'A Ruby String object'" the variable
's' IS a String object - it gives a name for a special String object, or
references a special String object (see 1).

Wolfgang Nádasi-Donner

(1): This wording is is used from a German, who tries to find out the best
English word for what in German will be called "benennt", or "bezeichnet". I
don't know what wording is really good in English.
 
D

dblack

---2049402039-757453356-1169918019=:20289
Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-757453356-1169918019=:20289"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

---2049402039-757453356-1169918019=:20289
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi--

I agree. reference/value came from the world before talking about objects= =2E I=20
remember in Algol60 somehow the wording "after 'integer i;' the variable = 'i=20
IS an integer".

We would never say that after writing "s =3D 'A Ruby String object'" the= =20
variable 's' IS a String object - it gives a name for a special String=20
object, or references a special String object (see 1).

There's also the distinction between references and immediate
values... though as Robert K. said, most of the differences are
hidden from the programmer (like, there's no extra level of
indirection you have to explicitly do).
Wolfgang N=E1dasi-Donner

(1): This wording is is used from a German, who tries to find out the bes= t=20
English word for what in German will be called "benennt", or "bezeichnet"= =2E I=20
don't know what wording is really good in English.

I think "betoken" is probably the closest etymologically to
"bezeichnen", but it's more archaic-sounding. "Designate" might be a
good match. (This is why we end up doing things like using
"reference" as a verb :)


David

--=20
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
---2049402039-757453356-1169918019=:20289--
---2049402039-757453356-1169918019=:20289--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,228
Messages
2,571,157
Members
47,785
Latest member
deepusaini

Latest Threads

Top