# A bizarre problem with data= methods

**URL:** <https://www.ruby-forum.com/t/a-bizarre-problem-with-data-methods/174479>\
**Category:** Ruby\
**Created:** [August 27, 2009, 11:06am UTC](https://www.ruby-forum.com/t/a-bizarre-problem-with-data-methods/174479 "2009-08-27T11:06:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![revlob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@revlob](https://www.ruby-forum.com/u/revlob)\
**Post date:** [August 27, 2009, 11:06am UTC](https://www.ruby-forum.com/t/a-bizarre-problem-with-data-methods/174479/1 "2009-08-27T11:06:35Z")

</div>

Hi all.

I have found an error that does not understand. Look this simple code:

* * *

class Foo  
def value=(data)  
puts “Look #{data}”  
end  
end

## f = Foo.new.value = “at me” # =\> Look at me

Great! A little assigner that works perfectly. But now I make a  
constructor that use the assigner directly:

* * *

class Foo  
def initialize(data)  
value = data  
end

def value=(data)  
puts “Look #{data}”  
end  
end

## f = Foo.new(“at me”) # =\> Nothing!!!

Why? The constructor doesn’t use value= method. What is happening? Is  
that normal?

Thanl you for your help.

---

<div class="post-metadata">

**Author:** ![revlob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@revlob](https://www.ruby-forum.com/u/revlob)\
**Post date:** [August 27, 2009, 11:18am UTC](https://www.ruby-forum.com/t/a-bizarre-problem-with-data-methods/174479/2 "2009-08-27T11:18:18Z")

</div>

2009/8/27 David E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> end  
> that normal?  
> You need an explicit self, ie. `self.value = data`. Without the `self`,  
> Ruby  
> interprets that line as a local variable assignment, not a method call.

---

<div class="post-metadata">

**Author:** ![revlob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@revlob](https://www.ruby-forum.com/u/revlob)\
**Post date:** [August 27, 2009, 11:18am UTC](https://www.ruby-forum.com/t/a-bizarre-problem-with-data-methods/174479/3 "2009-08-27T11:18:53Z")

</div>

On Thursday 27 August 2009, David E. wrote:

> |  
> | end  
> |that normal?  
> |  
> |Thanl you for your help.  
> |

Because when ruby sees something like:

identifier = value

it thinks you’re creating a local variable called identifier, not  
calling the  
identifier= method. To force ruby to understand you want to call the  
method,  
you need to use the dot notation, self.identifier = value, or use send:  
send(:identifier=, value).

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![revlob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@revlob](https://www.ruby-forum.com/u/revlob)\
**Post date:** [August 27, 2009, 12:50pm UTC](https://www.ruby-forum.com/t/a-bizarre-problem-with-data-methods/174479/4 "2009-08-27T12:50:47Z")

</div>

El jueves 27 de agosto, James C. escribiÃ³:

> You need an explicit self, ie. `self.value = data`. Without the  
> `self`, Ruby interprets that line as a local variable assignment, not  
> a method call.

It has sense. Other form, Ruby might search all methods of class in each  
local assignment.

Thank you.
