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
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 Koch 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
--
Vincent Fourmond, PhD student (not for long anymore)
http://vincent.fourmond.neuf.fr/
Everything is pass-by-reference in Ruby. There is no pass-by-value. So
your memory problems must have another cause.
Andy Koch 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
--
Posted via http://www.ruby-forum.com/\.
Strings are mutable objects and should be treated as such
def append_sour_word(strbuf)
strbuf << " ...uck!"
end
text = "Good l"
append_sour_word(text) #=> "Good luck" is returned AND is contained in text now
text #=> "Good luck"
def assign_concatenated(strbuf)
strbuf = strbuf + " ...uck!"
end
assign_concatenated(text) #=> "This is some new text" is returned but you lost the reference inside the function scope so the actual _value_ stays the same
text #=> still "Good luck", because you assigned another reference to your variable
def replace_content(strbuf)
strbuf.replace(strbuf + " ...uck!")
end
replace_content(text) "Good l..uck!..uck!", but now with a desired side effect
text #=> "Good l..uck!..uck!", now you replaced the object with another one
Basically, to avoid confusion - if you eating up RAM avoid object _duplication_, because that's what eats it up. But it's perfectly possible to
massage objects in place without having to ask them for their "actual value".
On Jan 27, 2007, at 12:27 AM, Andy Koch wrote:
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.
--
Julian 'Julik' Tarkhanov
please send all personal mail to
me at julik.nl
How about:
class Vault
attr_accessor :unvault
def initialize val
@unvault = val
end
end
class Object
def to_vault
Vault.new(self)
end
end
def change_me(obj)
obj.unvault += " rocks!"
end
o = "Ruby".to_vault
change_me(o)
p o.unvault
$ ruby vault-test.rb
"Ruby rocks!"
On 1/27/07, Andy Koch <andy.koch@pc-doctor.com> 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
Per Velschow 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
Hi --
On Sat, 27 Jan 2007, Per Velschow wrote:
Andy Koch 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
--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
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.
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 ?)
Those are still passed by reference. It just so happens that they are
unique and immutable in the system, so it looks a lot like
pass-by-value.
-austin
On 1/26/07, Vincent Fourmond <vincent.fourmond@9online.fr> wrote:
Andy Koch 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 ?)
--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
* austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
* austin@zieglers.ca
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
"Phrogz" <gavin@refinery.com> wrote/schrieb <1169857121.979437.61500@q2g2000cwa.googlegroups.com>:
On Jan 26, 4:32 pm, Vincent Fourmond <vincent.fourm...@9online.fr>
How do you know if those are passed by reference or by value?
def oi(obj)
obj.object_id
end
x = "yeah" # or set it to anything else
oi(x) == x.object_id # => true
Regards
Thomas
I think the problem in these discussions is that "reference" in the programming language jargon is not the same word as in "pass-by-reference". This discussion is common in Java forums as well. Java is pass-by-value.
In C++ since the copy constructor is involved, I found people in freenode#c++ consider &-arguments to be pass-by-reference because they can modify the objects in the caller, albeit the assigment test discussed in the thread does not work, that'd be
def m(b)
b = 7
end
a = 3
m(a)
# a expected to be 7 in pass-by-reference
-- fxn
On Jan 27, 2007, at 7:29 PM, Austin Ziegler wrote:
On 1/26/07, Vincent Fourmond <vincent.fourmond@9online.fr> wrote:
Andy Koch 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 ?)Those are still passed by reference. It just so happens that they are
unique and immutable in the system, so it looks a lot like
pass-by-value.
Hi --
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 Sat, 27 Jan 2007, Martin C. Martin wrote:
--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
Xavier Noria <fxn@hashref.com> wrote/schrieb <73286BF0-AFE1-40C1-9704-16A3E129CD56@hashref.com>:
In C++ since the copy constructor is involved, I found people in
freenode#c++ consider &-arguments to be pass-by-reference because
they can modify the objects in the caller, albeit the assigment test
discussed in the thread does not work, that'd bedef m(b)
b = 7
enda = 3
m(a)
# a expected to be 7 in pass-by-reference
That's another story. With the code above you rather reset b to a new
object instead of modifying the passed object. Look here:
def m(b)
b.shift
b.unshift(7)
end
a = [3]
m(a)
=> [7]
Would that be the same with pass-by-value? No, I don't think so.
Regards
Thomas
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
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"
endarr = [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).
Hi --
>
> 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"
endarr = [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).
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 1/27/07, dblack@wobblini.net <dblack@wobblini.net> wrote:
On Sat, 27 Jan 2007, Martin C. Martin wrote:
--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
--
"The best way to predict the future is to invent it."
- Alan Kay
Yes, that's the usual point in the discussion. Being able to change the internal state of mutable objects is not a test for pass-by-reference. Java is pass-by-value for instance (according to the JLS) and that test passes.
Problem with the assignment-seen-in-the-caller test is what dblack pointed out, it mixes assigment semantics.
In Perl, which is pass by reference, you can do this for example:
$ perl -wle '$x = 0; sub { ++$_[0] }->($x); print $x'
1
-- fxn
On Jan 27, 2007, at 8:25 PM, Thomas Hafner wrote:
That's another story. With the code above you rather reset b to a new
object instead of modifying the passed object. Look here:def m(b)
b.shift
b.unshift(7)
end
a = [3]
m(a)
=> [7]Would that be the same with pass-by-value? No, I don't think so.
Hi --
Xavier Noria <fxn@hashref.com> wrote/schrieb <73286BF0-AFE1-40C1-9704-16A3E129CD56@hashref.com>:
In C++ since the copy constructor is involved, I found people in
freenode#c++ consider &-arguments to be pass-by-reference because
they can modify the objects in the caller, albeit the assigment test
discussed in the thread does not work, that'd bedef m(b)
b = 7
enda = 3
m(a)
# a expected to be 7 in pass-by-referenceThat's another story. With the code above you rather reset b to a new
object instead of modifying the passed object. Look here:def m(b)
b.shift
b.unshift(7)
end
a = [3]
m(a)
=> [7]Would that be the same with pass-by-value? No, I don't think so.
It depends what value you're passing. If the value is an array, and
it gets copied into b, then it wouldn't work that way; but what's
happening is that a *reference* to an array is getting passed by
value. That value (which is a reference to an array) is assigned to
the variable a in the caller's scope, and also to the variable b
inside the method.
David
On Sun, 28 Jan 2007, Thomas Hafner wrote:
--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
Hi --
Hi --
>
> 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"
endarr = [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).Just to demonstrate your point
def modifying a_param
a_param = "hi"
enda_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
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
On Sat, 27 Jan 2007, Robert Dober wrote:
On 1/27/07, dblack@wobblini.net <dblack@wobblini.net> wrote:
On Sat, 27 Jan 2007, Martin C. Martin wrote:
--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
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"
endarr = [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?
David
--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)