[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: pass by reference?

Gavin Kistner

1/27/2007 12:19:00 AM

On Jan 26, 4:32 pm, Vincent Fourmond <vincent.fourm...@9online.fr>
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.

21 Answers

Martin C. Martin

1/27/2007 12:24:00 AM

0



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

dblack

1/27/2007 12:44:00 AM

0

Robert Klemme

1/27/2007 7:48:00 AM

0

On 27.01.2007 01:43, dblack@wobblini.net wrote:
> 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>

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

dblack

1/27/2007 11:05:00 AM

0

dblack

1/27/2007 11:11:00 AM

0

WoNáDo

1/27/2007 11:34:00 AM

0

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

WoNáDo

1/27/2007 11:35:00 AM

0

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

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

WoNáDo

1/27/2007 11:36:00 AM

0

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

dblack

1/27/2007 11:47:00 AM

0

Robert Klemme

1/27/2007 11:57:00 AM

0

On 27.01.2007 12:10, dblack@wobblini.net wrote:
> Hi --
>
> On Sat, 27 Jan 2007, Robert Klemme wrote:
>
>> On 27.01.2007 01:43, dblack@wobblini.net wrote:
>>> 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