# How to pass numeric variables by reference?

**URL:** <https://www.ruby-forum.com/t/how-to-pass-numeric-variables-by-reference/49841>\
**Category:** Ruby\
**Created:** [December 9, 2005, 5:23am UTC](https://www.ruby-forum.com/t/how-to-pass-numeric-variables-by-reference/49841 "2005-12-09T05:23:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [December 9, 2005, 5:23am UTC](https://www.ruby-forum.com/t/how-to-pass-numeric-variables-by-reference/49841/1 "2005-12-09T05:23:29Z")

</div>

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

void foo (int\* x)  
{

}

---

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [December 9, 2005, 5:42am UTC](https://www.ruby-forum.com/t/how-to-pass-numeric-variables-by-reference/49841/2 "2005-12-09T05:42:45Z")

</div>

From: Eric C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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”

---

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [December 9, 2005, 1:20pm UTC](https://www.ruby-forum.com/t/how-to-pass-numeric-variables-by-reference/49841/3 "2005-12-09T13:20:38Z")

</div>

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
