Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to pass arguments by reference in a function

Reply
Thread Tools

How to pass arguments by reference in a function

 
 
ashishwave
Guest
Posts: n/a
 
      07-27-2007
if i pass arguments in a function then they get passed by value. how
to get it pased by reference.
For example: if i want to swrite my own swap function (not using
multiple assignment syntax a,b=b,a) just for example example, how to
implement that in ruby

bye
Ashish Ranjan

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      07-27-2007
2007/7/27, ashishwave <>:
> if i pass arguments in a function then they get passed by value. how
> to get it passed by reference.


You can't. There are some solutions to this depending on situation:

1. inplace modification of arguments (Strings, Arrays, Hashes - all
sorts of containers)

2. multiple return values

3. work with instance variables

Generally in my experience it's not needed.

> For example: if i want to swrite my own swap function (not using
> multiple assignment syntax a,b=b,a) just for example example, how to
> implement that in ruby


irb(main):001:0> def swap(a,b) return b,a end
=> nil
irb(main):002:0> x,y=1,2
=> [1, 2]
irb(main):003:0> x,y = swap x,y
=> [2, 1]
irb(main):004:0> x
=> 2
irb(main):005:0> y
=> 1

But in reality you would just do

x,y = y,x

Kind regards

robert

 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      07-27-2007
ashishwave wrote:

> if i pass arguments in a function then they get passed by value.


Ruby supports two kinds of variables; IIRC numbers, characters, booleans,
and nil are "immediate", and everything else is a reference to an object.

The best way to explain this is to look at Ruby's source. A VALUE is the
union of a long and a pointer. Anything small enough to fit in a long is an
immediate value, and everything else uses the pointer to point to a
non-immediate object.

So at function call time, Ruby passes the _VALUE_ by value. So immediates
get copied, and objects get passed by reference.

So, in addition to your other answer, you could also put your referend into
a class, and pass this around. That might fit the ideals of Object Oriented
Programming better than passing immediates would.

--
Phlip
http://www.oreilly.com/catalog/9780596510657/
^ assert_xpath
http://tinyurl.com/23tlu5 <-- assert_raise_message
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      07-27-2007
Robert Klemme wrote:

> I think "pass by reference" is not the proper term because that would
> imply that you could change a variable in the calling scope, i.e. you
> could do
>
> def magic(x) x = 10 end
> foo = 1
> puts foo # prints 1
> magic(foo)
> puts foo # prints 10
>
> which you can't.


Working backwards from your example, that is the definition of an "immediate
value". Violent agreement achieved!

--
Phlip
http://www.oreilly.com/catalog/9780596510657/
^ assert_xpath
http://tinyurl.com/23tlu5 <-- assert_raise_message
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      07-27-2007
2007/7/27, Phlip <>:
> ashishwave wrote:
>
> > if i pass arguments in a function then they get passed by value.

>
> Ruby supports two kinds of variables; IIRC numbers, characters, booleans,
> and nil are "immediate", and everything else is a reference to an object.


Personally I find it easier to grasp (especially when starting out
with Ruby) this when you assume that everything is an object.
Although there are some optimizations going on behind the scenes all
objects behave the same - from a Ruby user's perspective. This is
totally different from Java's handling of PODs for example.

> The best way to explain this is to look at Ruby's source. A VALUE is the
> union of a long and a pointer. Anything small enough to fit in a long is an
> immediate value, and everything else uses the pointer to point to a
> non-immediate object.


Again, I would not start with Ruby sources here.

> So at function call time, Ruby passes the _VALUE_ by value. So immediates
> get copied, and objects get passed by reference.


I think "pass by reference" is not the proper term because that would
imply that you could change a variable in the calling scope, i.e. you
could do

def magic(x) x = 10 end
foo = 1
puts foo # prints 1
magic(foo)
puts foo # prints 10

which you can't. I'd rather call it "call by reference value", i.e.
the reference is copied.

> So, in addition to your other answer, you could also put your referend into
> a class, and pass this around. That might fit the ideals of Object Oriented
> Programming better than passing immediates would.


I am not sure why you refer to immediates here. Using instance
variables is generally one of the core OO techniques.

Kind regards

robert

 
Reply With Quote
 
Florian Gross
Guest
Posts: n/a
 
      07-27-2007
On Jul 27, 8:32 am, "Robert Klemme" <shortcut...@googlemail.com>
wrote:
> 2007/7/27, ashishwave <ashishw...@gmail.com>:
>
> > if i pass arguments in a function then they get passed by value. how
> > to get it passed by reference.

>
> You can't. There are some solutions to this depending on situation:
>
> 1. inplace modification of arguments (Strings, Arrays, Hashes - all
> sorts of containers)
>
> 2. multiple return values
>
> 3. work with instance variables


4. passing lambdas around

x, y = 1, 2
x_ref = [lambda { x }, lambda { |x| }]
y_ref = [lambda { y }, lambda { |y| }]
swap(x_ref, y_ref}

See http://flgr.0x42.net/code/variable.rb


 
Reply With Quote
 
Lloyd Linklater
Guest
Posts: n/a
 
      07-27-2007
ashishwave wrote:
> For example: if i want to write my own swap function (not using
> multiple assignment syntax a,b=b,a) just for example example, how to
> implement that in ruby


That reminds me, in the old days you could do a swap without a third
variable using XOR. e.g.

a = 13
b = 17

a ^= b
b ^= a
a ^= b

and they are swapped.

I know that Ruby might do it as mentioned above.

My question is, which is more efficient?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ian Whitlock
Guest
Posts: n/a
 
      07-27-2007
Lloyd Linklater wrote:
> My question is, which is more efficient?


It may depends on the definition of efficient.
Try it with a = "13" and b = "17".

In terms of execution time it is a horse race
that doesn't matter much. Try

require "benchmark"
include Benchmark

n = 100001

bm(10) do |x|
a = 13
b = 17
x.report("XOR") {n.times {a ^= b;b ^= a;a ^= b} }
puts a , b
x.report("ruby") {n.times {b,a = a,b} }
puts a , b
end

Ian
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      07-27-2007
On 27.07.2007 11:22, Phlip wrote:
> Robert Klemme wrote:
>
>> I think "pass by reference" is not the proper term because that would
>> imply that you could change a variable in the calling scope, i.e. you
>> could do
>>
>> def magic(x) x = 10 end
>> foo = 1
>> puts foo # prints 1
>> magic(foo)
>> puts foo # prints 10
>>
>> which you can't.

>
> Working backwards from your example, that is the definition of an "immediate
> value". Violent agreement achieved!


I'm not sure where you see the agreement and also I don't see how the
piece above makes for a definition of an immediate value. Did I miss a
smiley somewhere?

An immediate value is something that does not live on the heap but where
the reference is the value. Other than that it behaves the same as any
other object. Immediate values are just an interpreter internal
optimization - you wouldn't notice if that was abandoned and Fixnums
became ordinary instances like others (apart from performance of course).

The example presented by me does not work - regardless whether you use a
Fixnum, an Array or some other object. This has only to do with the
parameter passing mode.

Kind regards

robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper moreau.steve@gmail.com C Programming 3 12-31-2008 07:13 AM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
function default arguments from other arguments tutmann C++ 4 10-17-2006 08:00 PM
function call with arguments which takes no arguments Neo C Programming 10 01-20-2005 06:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57