Pass by reference?

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

Andy K. 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

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

Andy K. 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

Hi –

On Sat, 27 Jan 2007, Per V. wrote:

Andy K. 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.

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

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.

  • Martin

Per V. 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

On Jan 26, 4:32 pm, Vincent F. [email protected]
wrote:

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.

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

David

On 1/27/07, [email protected] [email protected] wrote:

If you passed an immutable type by reference, how would you know that
end
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

or to put it different
x = a.object_id
method a
a.object_id == x # for sure

Robert

David

On 27.01.2007 01:43, [email protected] wrote:

Is there any way for the function you’re calling to modify the value
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

Hi –

On Sat, 27 Jan 2007, Robert D. wrote:

Er, I mean…
obj << “hi”
I still wouldn’t call this pass by reference (see my earlier post in

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

Hi –

On Sat, 27 Jan 2007, Robert K. wrote:

change_me(arr)

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

On 1/27/07, [email protected] [email protected] wrote:

puts a # low

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

David, it is not my fault that parameter passing by value has the same
semantics as reference assignment :wink: (it probably is what is happening
behind the scenes, a new reference is allocated and the formal parameter
is
copied to it).
Do you see what I wanted to show?

Cheers
Robert

David

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

Output >>>>>

Passed by value
Passed by value
Passed by reference

EoE >>>>>

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

Hi –

On Sat, 27 Jan 2007, Wolfgang Nádasi-Donner wrote:

def modifying3(a_param)
else
else
else
puts “Passed away”
end

Output >>>>>

Passed by value
Passed by value

But you’ve broken the bindings. It’s a whole new ballgame, as we say
here :slight_smile:

Passed by reference

I would say: a reference, passed by value.

EoE >>>>>

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.

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

def x(a)
a = “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 = “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

On 27.01.2007 12:10, [email protected] wrote:

If you pass an immutable type by reference, does it make a sound?
obj << “hi”
I still wouldn’t call this pass by reference (see my earlier post in
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

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

[email protected] schrieb:

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.

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

Output >>>>>

Passed by value
Passed by value
Passed by reference

EoE >>>>>

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

Hi –

On Sat, 27 Jan 2007, Wolfgang Nádasi-Donner wrote:

any problems. If you change “someobject” this will have consequences for ‘a’,
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs