# Simple question: pointer equivalent in ruby

**URL:** <https://www.ruby-forum.com/t/simple-question-pointer-equivalent-in-ruby/197626>\
**Category:** Ruby\
**Created:** [October 17, 2010, 5:59pm UTC](https://www.ruby-forum.com/t/simple-question-pointer-equivalent-in-ruby/197626 "2010-10-17T17:59:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [October 17, 2010, 5:59pm UTC](https://www.ruby-forum.com/t/simple-question-pointer-equivalent-in-ruby/197626/1 "2010-10-17T17:59:11Z")

</div>

learned ruby a few months ago - is there a pointer equivalent in ruby  
for setting the variable referenced by the return value of a function,  
rather than just getting it?

like something that would allow you to do this.

def method  
@value  
end

@value = 5  
method = 3  
@value (should be 3)

OR even

value = 5  
othervalue = value  
othervalue = 3  
value (should be 3)

---

<div class="post-metadata">

**Author:** ![voxom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9a140/32.png) [@voxom](https://www.ruby-forum.com/u/voxom)\
**Post date:** [October 17, 2010, 6:15pm UTC](https://www.ruby-forum.com/t/simple-question-pointer-equivalent-in-ruby/197626/2 "2010-10-17T18:15:55Z")

</div>

Shea B. wrote in post #954956:

> OR even
> 
> value = 5  
> othervalue = value  
> othervalue = 3  
> value (should be 3)

That is precisely how ruby works by default. If you wanted othervalue to  
just take the ‘value’ of value, i.e. 3, you would use this instead:

value = 5  
other = value.dup  
puts other #5  
other = 3  
puts other #3  
puts value #5

---

<div class="post-metadata">

**Author:** ![voxom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9a140/32.png) [@voxom](https://www.ruby-forum.com/u/voxom)\
**Post date:** [October 17, 2010, 6:19pm UTC](https://www.ruby-forum.com/t/simple-question-pointer-equivalent-in-ruby/197626/3 "2010-10-17T18:19:15Z")

</div>

what about the first example with the method?

---

<div class="post-metadata">

**Author:** ![voxom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9a140/32.png) [@voxom](https://www.ruby-forum.com/u/voxom)\
**Post date:** [October 17, 2010, 6:29pm UTC](https://www.ruby-forum.com/t/simple-question-pointer-equivalent-in-ruby/197626/4 "2010-10-17T18:29:52Z")

</div>

Well generally for such situations you create setters, the example you  
gave  
includes only a getter.

def method=(val)  
@value = val  
end

that will then allow you to do method = 3 & that will in turn set @value  
to  
3.

Although I don’t see what exactly you wanted to know or what you’re  
example  
had to do with pointers…

–  
Thanks & Regards,  
Dhruva S…

---

<div class="post-metadata">

**Author:** ![voxom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9a140/32.png) [@voxom](https://www.ruby-forum.com/u/voxom)\
**Post date:** [October 17, 2010, 6:32pm UTC](https://www.ruby-forum.com/t/simple-question-pointer-equivalent-in-ruby/197626/5 "2010-10-17T18:32:24Z")

</div>

Hi –

On Mon, 18 Oct 2010, Parker S. wrote:

> value = 5  
> other = value.dup

Have you tried running that? 🙂

David

–  
David A. Black, Senior Developer, Cyrus Innovation Inc.

The Ruby training with Black/Brown/McAnally  
Compleat Stay tuned for next event!  
Rubyist [http://www.compleatrubyist.com](http://www.compleatrubyist.com)

---

<div class="post-metadata">

**Author:** ![voxom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9a140/32.png) [@voxom](https://www.ruby-forum.com/u/voxom)\
**Post date:** [October 17, 2010, 9:13pm UTC](https://www.ruby-forum.com/t/simple-question-pointer-equivalent-in-ruby/197626/6 "2010-10-17T21:13:56Z")

</div>

I was confused about the way that ruby assigns and stores variables.

I fixed my problem using setters and getters.

thanks for the help

---

<div class="post-metadata">

**Author:** ![voxom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9a140/32.png) [@voxom](https://www.ruby-forum.com/u/voxom)\
**Post date:** [October 17, 2010, 7:56pm UTC](https://www.ruby-forum.com/t/simple-question-pointer-equivalent-in-ruby/197626/7 "2010-10-17T19:56:07Z")

</div>

On 10/17/2010 06:22 PM, Shea B. wrote:

> what about the first example with the method?

method = 3 will always assign to a local variable. If you really want  
to produce other side effects you need to do

self.method = 3

The topic seems to come up more frequently in recent time. I don’t know  
whether Ruby community experiences increased migration from C++  
developers or what the reason for this is. I believe the fact that  
people search for something call by reference in said language indicates  
that they do not have adjusted to the Ruby mindset yet. Shea, if you  
provide more context we might be able to help you understand how the  
problem you are trying to solve is usually solved in Ruby - without call  
by reference.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![voxom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9a140/32.png) [@voxom](https://www.ruby-forum.com/u/voxom)\
**Post date:** [October 18, 2010, 2:22am UTC](https://www.ruby-forum.com/t/simple-question-pointer-equivalent-in-ruby/197626/8 "2010-10-18T02:22:20Z")

</div>

David A. Black wrote in post #954969:

> Hi –
> 
> On Mon, 18 Oct 2010, Parker S. wrote:
> 
> > value = 5  
> > other = value.dup
> 
> Have you tried running that? 🙂

Doh!

I really should have gone with a non nil, symbol, number example instead  
=)

Shea B. wrote:

> I fixed my problem using setters and getters.

If you’re now writing manual accessors (method, method=) definitely look  
at attr\_accessor, which will save you a lot of work.
