How to pass numeric variables by reference?

  • Thread starter Eric Christensen
  • Start date
E

Eric Christensen

What's the Ruby way of achieving pass-by-reference for numbers, as in
the C code

void foo (int* x)
{

}
 
R

rubikitch

From: Eric Christensen <[email protected]>
Subject: How to pass numeric variables by reference?
Date: Fri, 9 Dec 2005 13:23:36 +0900
What's the Ruby way of achieving pass-by-reference for numbers, as in
the C code

Array can do it.

def f(a)
a[0] = 10
end

a = [1]
f(a)
a[0] == 10 or raise
puts "ok"
 
L

Lou Vanek

probably assign on callback. In other words, Ruby doesn't have
a direct pass-by-reference operation for numbers,
but you can easily achieve the same result.

using irb:

10 :> def foo(c,d)
return c+1, d+2
end
==> nil
13 :> a,b = 5,6
==> [5, 6]
14 :> puts a,b
5
6
==> nil
15 :> a,b = foo(a,b)
==> [6, 8]
16 :> puts a,b
6
8
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top