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

R

Ralph Shnelvar

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.
 
R

Rick DeNatale

Newbie here.

Consider this irb session ...


F:\InstantRails-2.0-win\rails_apps>irb
irb(main):001:0> class X
irb(main):002:1> =A0 attr_accessor :x, :y
irb(main):003:1> end
=3D> nil
irb(main):004:0>
irb(main):005:0* x1 =3D X.new
=3D> #<X:0x29211a0>
irb(main):006:0>
irb(main):007:0* x1.x =3D 5;
irb(main):008:0* x1.y =3D 6;
irb(main):009:0*
irb(main):010:0*
irb(main):011:0*
irb(main):012:0* class X
irb(main):013:1> =A0 def x=3D(arg)
irb(main):014:2> =A0 =A0 puts "arg=3D" + arg.to_s
irb(main):015:2> =A0 =A0 @x =3D arg
irb(main):016:2> =A0 =A0 puts "whoopy"
irb(main):017:2> =A0 =A0 @x
irb(main):018:2> =A0 end
irb(main):019:1>
irb(main):020:1* =A0 def make_x
irb(main):021:2> =A0 =A0 self.x =3D "xyzzy"
irb(main):022:2> =A0 end
irb(main):023:1> end
=3D> nil
irb(main):024:0>
irb(main):025:0*
irb(main):026:0* x1.x =3D "hello"
arg=3Dhello
whoopy
=3D> "hello"
irb(main):027:0> x1
=3D> #<X:0x29211a0 @y=3D6, @x=3D"hello">
irb(main):028:0> x1.make_x
arg=3Dxyzzy
whoopy
=3D> "xyzzy"
irb(main):029:0> x1
=3D> #<X:0x29211a0 @y=3D6, @x=3D"xyzzy">
irb(main):030:0>



The background ...

I am debugging an RoR application and noticed that the @activation_code i=
nstance variable was being modified _somewhere_. =A0I couldn't tell where.
So I added a method to the User class

=A0def activation_code=3D(arg)
=A0 =A0puts at_file_line_msg(__FILE__, __LINE__)
=A0 =A0 =A0 =A0 =A0puts "arg=3D" + arg
=A0 =A0@activation_code=3Darg
=A0 =A0debugger
=A0 =A0@activation_code
=A0end


When I look at the value of @activation_code in the debugger (at the debu=
uger breakpoint), it has the right value ...
 
B

Brian Candler

Rick said:
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 Development with Rails book, look up 'facade
columns' in the index.
 
R

Ralph Shnelvar

Brian,

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


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 Development 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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top