[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

value by reference

Mohammad Khan

1/18/2005 9:49:00 PM

Before telling my issue, let me show my script:


def do_something(a, b, c)
a = a + 1
b = b + 2
c = c + 3
end
a, b, c = 5, 6, 7
puts "before do_something:"
puts "a : #{a}"
puts "b : #{b}"
puts "c : #{c}"
do_something(a, b, c)
puts "after do_something:"
puts "a : #{a}"
puts "b : #{b}"
puts "c : #{c}"



My issue:
I would like to see the value of a, b, c after calling #do_something as:
a = 6, b = 8, c = 10

how can I do it without making a, b, c an instance variable?
In other words, how can I send value by reference?


Thanks
MOhammad







68 Answers

tsawyer

1/18/2005 10:10:00 PM

0

I beleive it is by reference, the problem is you need to modify
inplace.

In this particular case you'll need an alternative to #+, b/c #+ is not
an inplace operator. But will you use, I'm not sure. #succ seems to
work inplace, but that's not to helpful. (I've a mind to RCR for a 'def
succ(n=1)', but that doesn't help you now)

It does seem like there should be clearer ways to deal with this. I
know .net for instance has very clear distinctions.

Assaph Mehr

1/18/2005 10:22:00 PM

0

It's probably easier if you're used to think in pointers / references.
Ignore for the moment that fixnums are special (immediate values).

> def do_something(a, b, c)
> a = a + 1
> b = b + 2
> c = c + 3
> end

do_something receives 3 references in the declaration. those references
are places in three method-local variables.
in the body, it changes the local-variables to references that point to
*other* objects.



> a, b, c = 5, 6, 7

you have created three local variables, referencing fixnum objects.

> puts "before do_something:"
> puts "a : #{a}"
> puts "b : #{b}"
> puts "c : #{c}"

you print the values of the objects referenced by the local variables.

> do_something(a, b, c)

you called do_something, which received the references, and changed *in
the method body only* the method-local variables to point to other
objects.

> puts "after do_something:"
> puts "a : #{a}"
> puts "b : #{b}"
> puts "c : #{c}"

here you print the values referenced by the outside-scope local
variables. these local-variables have never been changed in this scope
so they point to the same fixnum objects you specified in the
beginning.


> My issue:
> I would like to see the value of a, b, c after calling #do_something
as:
> a = 6, b = 8, c = 10
>
> how can I do it without making a, b, c an instance variable?
> In other words, how can I send value by reference?

you have some options:
- global variables ($var): ugly and not recommended.
- return the new values: probably cleanest way.

def do_something(a, b, c)
a = a + 1
b = b + 2
c = c + 3
[a, b, c]
end
a, b, c = 5, 6, 7
a, b, c = do_something(a, b, c)

- create a box class: essentially a pointer. both the ouside-scope and
the method-scope local-variables point to the same box object so you
can change fields in the box object and those changes will be reflected
in all scopes.

HTH,
Assaph

Mohammad Khan

1/18/2005 10:38:00 PM

0

On Tue, 2005-01-18 at 17:16, Florian Gross wrote:
> Mohammad Khan wrote:
>
> > Before telling my issue, let me show my script:
> >
> > def do_something(a, b, c)
> > a = a + 1
> > b = b + 2
> > c = c + 3
> > end
> > a, b, c = 5, 6, 7
> > puts "before do_something:"
> > I would like to see the value of a, b, c after calling #do_something as:
> > a = 6, b = 8, c = 10
> >
> > how can I do it without making a, b, c an instance variable?
> > In other words, how can I send value by reference?
>
> This is not easily done. First of all you should ask yourself why you
> find yourself needing it and if there is not a better mechanism in Ruby
> that you could use to accomplish your goal. (Methods can return multiple
> values.)
>

I really need it.
I could do it by:

def do_something(a, b, c)
a = a + 1
b = b + 2
c = c + 3
return [a , b, c]
end

a, b, c = do_something(a, b, c)

I don't like to handle with too many return variables but I had to do it
in my real project.



> After that there's two approaches to addressing the above:
>
> 1) Supply lambdas:
>
> def do_something(inc_a, inc_b, inc_c)
> inc_a.call(1)
> inc_b.call(1)
> inc_c.call(1)
> end
>
> do_something(
> lambda { |x| a += x },
> lambda { |x| b += x },
> lambda { |x| c += x }
> )

What is lambdas? sorry.. if I am asking anything very strange !!
I will still prefer to return multiple variables in stead of using these
much code!


> 2) Use variable.rb to handle variables as Objects:
>
> def do_something(a, b, c)
> a.value += 1
> b.value += 1
> c.value += 1
> end
>
> do_something(Variable[:a], Variable[:b], Variable[:c])
>
> If you find yourself really needing to do this, please follow up with
> your reasons. I've not yet found many cases where this is necessary and
> would be interested in getting to know about new ones.




tsawyer

1/19/2005 12:33:00 AM

0

I would like to elaborate on this generally. Is ti possibel to have a
clean distinction? In other words, would to be possible to have all
objects to be modifiable inplace somehow? I know there are pathces out
there that do this, but what keeps this from being generally
acceptable?

T.

dblack

1/19/2005 1:39:00 AM

0

tsawyer

1/19/2005 3:12:00 AM

0

Sorry. #succ does not act inplace and nothing does on Fixnum. Its been
a while since I used VB.Net but in it you can define a subroutine with
parameters ByVal or ByRef:

Private Sub IntegerByRef(ByRef X As Integer)
Dim i As Long
For i = 1 To m_NumAssignments
X = 123
Next i
End Sub

Private Sub IntegerByVal(ByVal X As Integer)
Dim i As Long
For i = 1 To m_NumAssignments
X = 123
Next i
End Sub

Those are the two ways in VB, but Ruby is sort of inbetween. It passes
by reference, but if you reassign it looses the reference. You can
simulate by value simply by duplicating the parameter when it comes in,
but to do the other requires some trickery. One way is:

def do_somthing(a,b,c)
a[0] += 1
b[0] += 1
c[0] += 1
end
a, b, c = [5], [6], [7]
do_something(a,b,c)

But I wonder, could Ruby offer something like the VB forms without
violating immutability? Sort of an indirect reference.
Guess I don't understand why it's conidered a negative.

T.

Florian Gross

1/19/2005 12:11:00 PM

0

trans. wrote:

> Sorry. #succ does not act inplace and nothing does on Fixnum.

Of course nothing can change the value of immediate Objects. :)

> But I wonder, could Ruby offer something like the VB forms without
> violating immutability? Sort of an indirect reference.
> Guess I don't understand why it's conidered a negative.

See my earlier posting about the lambda { } and Variable[:name] ways of
doing this.

Ruby itself just treats variables as names for Objects. You can't really
refer to a variable itself, you can only refer to the Object it is a
name for. That's a surprisingly simple and non-confusing model. I think
Ruby itself should not be changed. (Though it would be nice if it had
something like Binding.of_caller built-in...)

Glenn Parker

1/19/2005 2:04:00 PM

0

Florian Gross wrote:
>
> See my earlier posting about the lambda { } and Variable[:name] ways of
> doing this.

Sorry, but I can't believe anybody would actually want to use the lambda
styles as proposed. They are ugly and obscure.

> Ruby itself just treats variables as names for Objects. You can't really
> refer to a variable itself, you can only refer to the Object it is a
> name for. That's a surprisingly simple and non-confusing model.

Judging by the frequency that this issue is discussed, it's more
confusing than you suggest. A typical programmers expects
call-by-reference to work one way, and Ruby works differently. Extra
confusion results because this difference is masked by using
self-updating methods, but it always fails for immediate objects, and it
eventually fails in a surprising way for non-immediate objects. I call
it surprising because a typical programmer does not expect the
assignment operator to destroy call-by-reference, but that is exactly
what happens.

def inc1(a, i); a += i; end
def inc2(a, i); a = a + i; end

To the naive Ruby programmer, inc1 and inc2 seem to be equivalent, but
Ruby gurus just shake their heads and sigh while they explain, yet
again, that there is a difference. Why is this considered a good thing? :)

You might say that the typical programmer has grown accustomed to a
confusing model, and Ruby is better off without that model. But, that
is nigh on saying that Ruby is better off without the typical
programmer. And that would be a shame, since Ruby has much to offer the
hordes of Perl and Python programmers looking for something better.

Ruby does *not* support call-by-reference, in the traditional sense of
the term. Instead, it is strictly call-by-value, and formal paremeters
are copies of references from the calling scope. If Ruby had real
call-by-reference, then it would be trivial to define a procedure that
updates a variable from its parent's scope, and inc1 and inc2 above
would be equivalent.

> I think
> Ruby itself should not be changed. (Though it would be nice if it had
> something like Binding.of_caller built-in...)

Tcl is similar to Ruby, regarding both the style of variable passing and
the resulting confusion among its new adopters, but Tcl documentation
has always stressed the details of this issue, and the Tcl core has the
"uplevel" and "upvar" builtins. I'm not a huge fan of Tcl, but I do
think the Tcl folks handled this issue in the best way possible.

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoi...


tsawyer

1/19/2005 2:05:00 PM

0

Right. I'm not suggesting that Ruby change this. I'm just wondering if
might be possible to add an extra feature that would allow for the
alternate when passing arguments through methods.

Personally, I've alwasy thought of variables as containters, so in that
way of thinking, which I think is common, one would expect to be able
to change what's in the container. I don't think Ruby's approach is
always simple for the enduser and it does lead to some surprises --for
example this very thread.

Hmm..reminds me of why I like the idea of all variables being
collections....

Binding.of_caller is useful for meta-programming, but that's bad
business for general use.

T.

dblack

1/19/2005 2:33:00 PM

0