# Something changed an instance variable ... and now I'm confused

**URL:** <https://www.ruby-forum.com/t/something-changed-an-instance-variable-and-now-im-confused/181281>\
**Category:** Ruby\
**Created:** [January 8, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/something-changed-an-instance-variable-and-now-im-confused/181281 "2010-01-08T18:05:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 8, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/something-changed-an-instance-variable-and-now-im-confused/181281/1 "2010-01-08T18:05:48Z")

</div>

Newbie here.

Consider this irb session …

F:\InstantRails-2.0-win\rails\_apps\>irb  
irb(main):001:0\> class X  
irb(main):002:1\> attr\_accessor :x, :y  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\>  
irb(main):005:0\* x1 = X.new  
=\> #\<X:0x29211a0\>  
irb(main):006:0\>  
irb(main):007:0\* x1.x = 5;  
irb(main):008:0\* x1.y = 6;  
irb(main):009:0\*  
irb(main):010:0\*  
irb(main):011:0\*  
irb(main):012:0\* class X  
irb(main):013:1\> def x=(arg)  
irb(main):014:2\> puts “arg=” + arg.to\_s  
irb(main):015:2\> @x = arg  
irb(main):016:2\> puts “whoopy”  
irb(main):017:2\> @x  
irb(main):018:2\> end  
irb(main):019:1\>  
irb(main):020:1\* def make\_x  
irb(main):021:2\> self.x = “xyzzy”  
irb(main):022:2\> end  
irb(main):023:1\> end  
=\> nil  
irb(main):024:0\>  
irb(main):025:0\*  
irb(main):026:0\* x1.x = “hello”  
arg=hello  
whoopy  
=\> “hello”  
irb(main):027:0\> x1  
=\> #\<X:0x29211a0 @y=6, @x=“hello”\>  
irb(main):028:0\> x1.make\_x  
arg=xyzzy  
whoopy  
=\> “xyzzy”  
irb(main):029:0\> x1  
=\> #\<X:0x29211a0 @y=6, @x=“xyzzy”\>  
irb(main):030:0\>

The background …

I am debugging an RoR application and noticed that the @activation\_code  
instance variable was being modified _somewhere_. I couldn’t tell  
where.

So I added a method to the User class

def activation\_code=(arg)  
puts at\_file\_line\_msg( **FILE** , **LINE** )  
puts “arg=” + arg  
@activation\_code=arg  
debugger  
@activation\_code  
end

When I look at the value of @activation\_code in the debugger (at the  
debuuger breakpoint), it has the right value …

but when I look at self in the debugger, the activation\_code field is  
nil.

I do not understand why things seem to work in the snippet of code I  
provided, above, in irb … but do not work in the far more complex RoR  
environment.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 8, 2010, 9:34pm UTC](https://www.ruby-forum.com/t/something-changed-an-instance-variable-and-now-im-confused/181281/2 "2010-01-08T21:34:12Z")

</div>

On Fri, Jan 8, 2010 at 12:05 PM, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> irb(main):004:0\>  
> irb(main):014:2\> puts “arg=” + arg.to\_s  
> irb(main):024:0\>  
> =\> “xyzzy”  
> So I added a method to the User class  
> When I look at the value of @activation\_code in the debugger (at the debuuger breakpoint), it has the right value …
> 
> but when I look at self in the debugger, the activation\_code field is nil.
> 
> I do not understand why things seem to work in the snippet of code I provided, above, in irb … but do not work in the far more complex RoR environment.

Is activation\_code an attribute of an ActiveRecord model?

If so then the value isn’t stored in an instance variable, the  
accessors are actually handled via method\_missing. By defining  
activation\_code= you are preventing activerecord from doing it’s  
thing.

You need something like:

def activation\_code=(arg)  
puts at\_file\_line\_msg( **FILE** , **LINE** )  
puts “arg=” + arg  
self[‘activation\_code’] =arg  
debugger  
arg # strictly speaking you don’t really need this since ruby  
assignments via an x= method ignore the return value  
end

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [Rick DeNatale - Developer - IBM | LinkedIn](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 8, 2010, 9:45pm UTC](https://www.ruby-forum.com/t/something-changed-an-instance-variable-and-now-im-confused/181281/3 "2010-01-08T21:45:32Z")

</div>

Rick Denatale wrote:

> Is activation\_code an attribute of an ActiveRecord model?
> 
> If so then the value isn’t stored in an instance variable

I believe it’s stored in @attributes (which is a Hash)

> You need something like:
> 
> def activation\_code=(arg)  
> puts at\_file\_line\_msg( **FILE** , **LINE** )  
> puts “arg=” + arg  
> self[‘activation\_code’] =arg  
> debugger  
> arg # strictly speaking you don’t really need this since ruby  
> assignments via an x= method ignore the return value  
> end

Should be cleaner to use: write\_attribute(‘activation\_code’, arg)

If you have the Agile Web D. with Rails book, look up ‘facade  
columns’ in the index.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 9, 2010, 6:44am UTC](https://www.ruby-forum.com/t/something-changed-an-instance-variable-and-now-im-confused/181281/4 "2010-01-09T06:44:17Z")

</div>

Brian,

Friday, January 8, 2010, 1:45:34 PM, you wrote:

BC\> Rick Denatale wrote:

> > Is activation\_code an attribute of an ActiveRecord model?

> > If so then the value isn’t stored in an instance variable

BC\> I believe it’s stored in @attributes (which is a Hash)

> > You need something like:

> > def activation\_code=(arg)  
> > puts at\_file\_line\_msg( **FILE** , **LINE** )  
> > puts “arg=” + arg  
> > self[‘activation\_code’] =arg  
> > debugger  
> > arg # strictly speaking you don’t really need this since ruby  
> > assignments via an x= method ignore the return value  
> > end

BC\> Should be cleaner to use: write\_attribute(‘activation\_code’, arg)

BC\> If you have the Agile Web D. with Rails book, look up  
‘facade  
BC\> columns’ in the index.

To both Brian and Rick, thank you.

Is there another way to determine why a particular value changed and/or  
when it is assigned to?
