From: fxn@... Date: 2007-01-28T18:15:05+09:00 Subject: Re: pass by reference? > dblack@wobblini.net writes: > >> 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? >> > > *** NEWBIE WARNING *** > > I'm entering this thread as it gives me a chance to test my own > understanding. > My apologies if I've missed the mark! > > I agree the example doesn't show pass by reference v pass by value. I > think it > shows more about scope. > > However, I think what he was trying to show was that if what was being > passed > was the address of the variable in the callers environment, then changes > to > what that variable pointed at would be seen in the callers environment as > well. > > i.e. > > a = Array.new could look like this > > ------- > | a | ---------- > | ----------->|Array Obj| > | | ---------- > ------- Addr = 1000 > Addr = 0 > > Here 'a' holds the value 1000, the address of the array object. This value > (1000) is stored in location 0. 'a' points to the storage location. In C > it > would be called a pointer, in ruby, its just a variable because yo don't > have > anything else. some would say 'a' is bound to the address 1000, which is > the > start location of an array object. > > Some of this confusion about passed by value and passed by reference is > due to > C and how it worked. In C, by default, things are passed by value > Ruby takes a different approach to C. In ruby, all variables are really > references to objects stored somewhwere else. The overheads associated > with > arguement passing are the same regardless of the size or type of object > the > variable references - its just an address. This means the traditional C > pass by > reference I think those statements were informal, but just for the archives C only has pass-by-value. Sometimes people think that, say, modifying an integer through a pointer argument shows pass-by-reference. But it doesn't, the argument is the pointer there, and is passed by value. -- fxn