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

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.

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

···

On Fri, Jan 8, 2010 at 12:05 PM, Ralph Shnelvar <ralphs@dos32.com> wrote:

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.

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

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

···

--
Posted via http://www.ruby-forum.com/\.

Brian,

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

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 Development with Rails book, look up 'facade
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?