# Calling a reader from a writer

**URL:** <https://www.ruby-forum.com/t/calling-a-reader-from-a-writer/72093>\
**Category:** Ruby\
**Created:** [October 14, 2006, 3:26am UTC](https://www.ruby-forum.com/t/calling-a-reader-from-a-writer/72093 "2006-10-14T03:26:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![clintpachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/977dab/32.png) [@clintpachl](https://www.ruby-forum.com/u/clintpachl)\
**Post date:** [October 14, 2006, 3:26am UTC](https://www.ruby-forum.com/t/calling-a-reader-from-a-writer/72093/1 "2006-10-14T03:26:53Z")

</div>

class Test  
def x  
@x + ‘\_instance’  
end

def x=(v)  
@x = v  
self.x # also tried `x’  
end  
end

t = Test.new  
puts t.x=(‘test\_x’)  
=\> test\_x  
puts t.x  
=\> test\_x\_instance

Why doesn’t the first puts output ‘test\_x\_instance’? I would think that  
the self.x call in the writer would call the reader.

How can one call the reader from the writer method?

-pachl

---

<div class="post-metadata">

**Author:** ![clintpachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/977dab/32.png) [@clintpachl](https://www.ruby-forum.com/u/clintpachl)\
**Post date:** [October 14, 2006, 4:00am UTC](https://www.ruby-forum.com/t/calling-a-reader-from-a-writer/72093/2 "2006-10-14T04:00:55Z")

</div>

Hi,

At Sat, 14 Oct 2006 10:25:11 +0900,  
clintpachl wrote in [ruby-talk:219643]:

> t = Test.new  
> puts t.x=(‘test\_x’)  
> =\> test\_x  
> puts t.x  
> =\> test\_x\_instance

An assignment expression always returns the assigned value,  
regardless of the implementation of the setter method.

---

<div class="post-metadata">

**Author:** ![clintpachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/977dab/32.png) [@clintpachl](https://www.ruby-forum.com/u/clintpachl)\
**Post date:** [October 14, 2006, 5:03am UTC](https://www.ruby-forum.com/t/calling-a-reader-from-a-writer/72093/3 "2006-10-14T05:03:08Z")

</div>

Hi –

On Sat, 14 Oct 2006, clintpachl wrote:

> t = Test.new  
> puts t.x=(‘test\_x’)  
> =\> test\_x  
> puts t.x  
> =\> test\_x\_instance
> 
> Why doesn’t the first puts output ‘test\_x\_instance’? I would think that  
> the self.x call in the writer would call the reader.

This just came up today on IRC. I’d forgotten about it, but was  
reminded.

The writer method allows you to use the assignment-like syntax:

t.x = value

Since the goal of this is to make the method call look and feel more  
assignment-like, and assignments return their right-hand side, the  
writer-method calls also return their right-hand side, rather than the  
last value in the method.

> How can one call the reader from the writer method?

You are calling the reader, but the magic rhs value overrides it. I  
haven’t found any way to circumvent it.

David

---

<div class="post-metadata">

**Author:** ![clintpachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/977dab/32.png) [@clintpachl](https://www.ruby-forum.com/u/clintpachl)\
**Post date:** [October 14, 2006, 11:41am UTC](https://www.ruby-forum.com/t/calling-a-reader-from-a-writer/72093/4 "2006-10-14T11:41:44Z")

</div>

On Oct 13, 8:02 pm, removed\_email\_address@domain.invalid wrote:

> > end
> 
> You are calling the reader, but the magic rhs value overrides it. I  
> haven’t found any way to circumvent it.

David, you are right on. Now that I think about it, Ruby does the  
natural thing. I was just experimenting and thought it was weird that  
my return value was not returning. I guess I needed to step outside the  
box.

-pachl
