Value by reference

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

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.

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

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

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.

variable.rb (1000 Bytes)

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

Hi --

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?

Fixnums are immediate values, and are immutable, even when they're
wrapped in variables. So what you're trying to do is equivalent to:

   do_something(5,6,7)

   puts 5 * 5 # 36 -- i.e., 5 has become 6

It won't work (luckily :slight_smile: because Ruby doesn't let you perform that
kind of modification on Fixnums.

With mutable objects you can do what you're describing -- for example,
adding to a string that's in a local variable:

   irb(main):001:0> def x(y); y << "def"; end
   => nil
   irb(main):002:0> s = "abc"
   => "abc"
   irb(main):003:0> x(s)
   => "abcdef"
   irb(main):004:0> s
   => "abcdef"

David

···

On Wed, 19 Jan 2005, Mohammad Khan wrote:

--
David A. Black
dblack@wobblini.net

#!/usr/bin/env ruby

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

def another_do_something(foo=Foo.new)
  foo.do_something
end

class Foo
  def initialize
    @value = 0
  end

  def value
    return @value
  end

  def value=(val)
    @value = val
  end

  def do_something
    @value += 1
    @value += 2
    @value += 3
  end
end

a, b, c = 5, 6, 7
f = Foo.new

# scenario 1
puts "before do_something : #{a.inspect}, #{b.inspect}, #{c.inspect}"
do_something( a, b, c)
puts "after do_something : #{a.inspect}, #{b.inspect}, #{c.inspect}" # a, b, c not changed
#do_something( a.clone, b.clone, c.clone)
#puts "after clone : #{a.inspect}, #{b.inspect}, #{c.inspect}" # can't clone Fixnum

# scenario 2
puts "before : #{f.inspect}"
another_do_something(f)
puts "after with f : #{f.inspect}" # f changed
another_do_something(f.clone)
puts "after with f.clone : #{f.inspect}" # f not changed, it make sense

What is the difference between scenario 1 and scenario 2?
What is making scenario 1 not changed?

If everything is object, Why can't I do
do_something(a.clone, b.clone, c.clone) # I don't want them to be changed
do_something(a, b, c) # change a, b, c like other objects

Hope, I could explain properly what I am trying to understand.

MOhammad

What I was missing, this thread made me to understand that.

a = 5
puts a.class -> Fixnum

As I knew everything is object in Ruby.. I thought a = 5 would do something like Fixnum.new(5) internally.
and 'a' would remain as object. I was wrong at this point.

Thanks everybody, specially Florian and Austin for their nice explanation.

I am out of this tread.

Mohammad

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!

···

On Tue, 2005-01-18 at 17:16, Florian Gross wrote:

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.

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.

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.

Mohammad Khan wrote:

do_something(a, b, c) # change a, b, c like other objects

Let me give an example:

x, y, z = 1, 2, 3
do_something(x, y, z) # completely equivalent to
do_something(1, 2, 3)

do_something() does not get variables. It gets the values the variables refer to. (It gets the objects the variables are names for.)

Therefore it can not change the variables, they are nothing physical.

variable.rb lets you turn variables *themselves* into Objects that can be passed to methods.

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

def another_do_something(foo = Foo.new)
  foo.do_something
end

class Foo
  def initialize
    @value = 0
  end

  def value
    return @value
  end
  
  def value=(val)
    @value = val
  end
  
  def do_something
    @value += 1
    @value += 2
    @value += 3
  end
end

a, b, c = 5, 6, 7
f = Foo.new

[...]

If everything is object, Why can't I do
  do_something(a.clone, b.clone, c.clone) # I don't want them to
                                          # be changed
  do_something(a, b, c) # change a, b, c like
                                          # other objects
Hope, I could explain properly what I am trying to understand.

I don't know why you're not getting this, as it is very simple.

a, b, and c are not objects. They're variables.

When you call Foo#do_something, you're changing the internal state
of an object. When you call Object#do_something, you're passing
three variables that can refer to just about any object.

It's no different than if I defined do_something as:

  def do_something(a, b, c)
    a += Foo.new
    b += Foo.new
    c += Foo.new
  end

As long as Foo can be added to the values of a, b, and c, I'm going
to get three completely new objects.

Try it with this:

  def do_something(a, b, c)
    a += "-half"
    b += "-thirds"
    c += "-fourths"
    puts a.id, b.id, c.id
    [ a, b, c ]
  end

  def do_something2(a, b, c)
    a << "-half"
    b << "-thirds"
    c << "-fourths"
    puts a.id, b.id, c.id
    [ a, b, c ]
  end

  a = "one"
  b = "two"
  c = "three"

  puts "#{a}<#{a.id}>, #{b}<#{b.id}>, #{c}<#{c.id}>"

  x, y, z = do_something(a, b, c)
  puts "#{x}<#{x.id}>, #{y}<#{y.id}>, #{z}<#{z.id}>"
  puts "#{a}<#{a.id}>, #{b}<#{b.id}>, #{c}<#{c.id}>"

  x, y, z = do_something2(a, b, c)
  puts "#{a}<#{a.id}>, #{b}<#{b.id}>, #{c}<#{c.id}>"
  puts "#{x}<#{x.id}>, #{y}<#{y.id}>, #{z}<#{z.id}>"

This will help you understand. #<< modifies a String in-place. It's
concatenation. += concatenates and creates a new string.

-austin

···

On Thu, 20 Jan 2005 01:10:22 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:
--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Mohammad Khan wrote:

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.

Maybe you could return a Hash then?

1) Supply lambdas:

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!

lambdas are objectified blocks. You can pass them around, assign them to variables, call methods and more of the stuff you can do with Objects.

The above might look like much code, but there's millions way of making it shorter. Here's one:

do_something { |ai, bi, ci| a += ai; b += bi; c += ci }

Invokation in the method would change to yield(1, 1, 1).

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.

I'm still interested in your reasons for doing this. :slight_smile:

trans. wrote:

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

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

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

I understand that.
But what about scenario 2?
Scenario 2 is not acting as scenario 1, is it because scenario 2 gets
object rather than variable?
Or, Scenario 2 gets clonable object while scenario 1 not?
Thanks, Florian.

···

On Wed, 2005-01-19 at 12:01, Florian Gross wrote:

Mohammad Khan wrote:

> do_something(a, b, c) # change a, b, c like other objects

Let me give an example:

x, y, z = 1, 2, 3
do_something(x, y, z) # completely equivalent to
do_something(1, 2, 3)

do_something() does not get variables. It gets the values the variables
refer to. (It gets the objects the variables are names for.)

--
Mohammad

Therefore it can not change the variables, they are nothing physical.

variable.rb lets you turn variables *themselves* into Objects that can
be passed to methods.

> def do_something(a, b, c)
> a += 1
> b += 2
> c += 3
> end
>
> def another_do_something(foo = Foo.new)
> foo.do_something
> end
>
> class Foo
> def initialize
> @value = 0
> end
>
> def value
> return @value
> end
>
> def value=(val)
> @value = val
> end
>
> def do_something
> @value += 1
> @value += 2
> @value += 3
> end
> end

> a, b, c = 5, 6, 7
> f = Foo.new
[...]
> If everything is object, Why can't I do
> do_something(a.clone, b.clone, c.clone) # I don't want them to
> # be changed
> do_something(a, b, c) # change a, b, c like
> # other objects
> Hope, I could explain properly what I am trying to understand.

I don't know why you're not getting this, as it is very simple.

May be I am not that simple!! :stuck_out_tongue:

a, b, and c are not objects. They're variables.

Isn't everything *object* in Ruby?

Now you know, what I am after ?!

···

On Wed, 2005-01-19 at 12:05, Austin Ziegler wrote:

On Thu, 20 Jan 2005 01:10:22 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:

--
MOhammad

When you call Foo#do_something, you're changing the internal state
of an object. When you call Object#do_something, you're passing
three variables that can refer to just about any object.

It's no different than if I defined do_something as:

  def do_something(a, b, c)
    a += Foo.new
    b += Foo.new
    c += Foo.new
  end

As long as Foo can be added to the values of a, b, and c, I'm going
to get three completely new objects.

Try it with this:

  def do_something(a, b, c)
    a += "-half"
    b += "-thirds"
    c += "-fourths"
    puts a.id, b.id, c.id
    [ a, b, c ]
  end

  def do_something2(a, b, c)
    a << "-half"
    b << "-thirds"
    c << "-fourths"
    puts a.id, b.id, c.id
    [ a, b, c ]
  end

  a = "one"
  b = "two"
  c = "three"

  puts "#{a}<#{a.id}>, #{b}<#{b.id}>, #{c}<#{c.id}>"

  x, y, z = do_something(a, b, c)
  puts "#{x}<#{x.id}>, #{y}<#{y.id}>, #{z}<#{z.id}>"
  puts "#{a}<#{a.id}>, #{b}<#{b.id}>, #{c}<#{c.id}>"

  x, y, z = do_something2(a, b, c)
  puts "#{a}<#{a.id}>, #{b}<#{b.id}>, #{c}<#{c.id}>"
  puts "#{x}<#{x.id}>, #{y}<#{y.id}>, #{z}<#{z.id}>"

This will help you understand. #<< modifies a String in-place. It's
concatenation. += concatenates and creates a new string.

-austin

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? :slight_smile:

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.tetrafoil.com/&gt;

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.

Mohammad Khan wrote:

But what about scenario 2? [methods changes state of an Object that
it gets as an argument] Scenario 2 is not acting as scenario 1, is it
because scenario 2 gets object rather than variable? Or, Scenario 2
gets clonable object while scenario 1 not?

First, let's agree on using the term "mutable" (or "changeable") instead
of "cloneable". (Immutable objects can not be cloned most of the time,
but the both terms are not strictly the same things.)

Scenario 2 changes attributes of an Object. This is something that is
entirely possible. It makes sense for a CarFactory object to be stopped
or running -- it also makes sense that you are able to stop or (re)start
such an object at run time. What can not be done is changing the value
of a variable that appeared in the method invocation because the method
does not know anything about the variable -- it gets the Object. Objects
can have state that can be changed. This can happen via accessors that
are invoked like obj.foo = x or other methods like car_factory.stop.
But while obj.foo looks similar to variable assignment it is something
entirely else.

Why do different things look so similar in Ruby? It's uniform access at
work.

In other languages there usually is the distinction between Object's
fields and methods. Having public fields is usually a bad idea (it
breaks encapsulation) and looks like this:

   obj.field += 5

Having methods that modify state looks like this:

   obj.setField(obj.getField() + 5)

In Ruby both look like this:

   obj.field += 5

Even if obj.field=() or obj.field() do not directly map to an instance
variable at all like in this case:

   def obj.radius()
     @diameter / 2
   end

   def obj.radius=(new_radius)
     @diameter = new_radius * 2
   end

I hope this explains how Ruby works. If there is still anything left
that you don't understand, feel free to ask.

Mohammad Khan wrote:

a, b, and c are not objects. They're variables.

Isn't everything *object* in Ruby?

Now you know, what I am after ?!

Every thing is an Object. Names for things are not Objects because then it would not be clear whether you are doing something with the name itself or with the Object it refers to. My variable.rb code lets you refer to the names themselves instead of their value with the Variable[:x] syntax.

Perl and PHP IIRC do this with the \$foo syntax (and C# AFAIK uses the 'ref' keyword for this, but don't quote me on this), but I think we should not have something like that in Ruby.