From: "David A. Black" Date: 2009-09-24T11:55:23+09:00 Subject: Re: Monkey Patching 2 Methods, Overrides One Method, Not The Other --1926193751-1692545108-1253760909=:30054 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-1692545108-1253760909=:30054" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-1692545108-1253760909=:30054 Content-Type: TEXT/PLAIN; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8BIT Hi -- On Thu, 24 Sep 2009, MaggotChild wrote: > On Sep 17, 5:02�am, "David A. Black" wrote: >> I think I'd have to see >> code that exhibits the problem you're describing in order to analyze > > This is suited for the Rails group, but the problem can be seen in its > simplest form here: > > #config/initializers/ar_attributes.rb > > module ActiveRecord > module AttributeMethods > > alias_method :ar_read_attribute, :read_attribute > def read_attribute(attr_name) > p "read_override" > ar_read_attribute(attr_name) > end > > alias_method :ar_write_attribute, :write_attribute > def write_attribute(attr_name, value) > raise 'You made it!' > end > end > end > > In the Rails console: > >>> person.read_attribute :name > "read_override" > => "Joe" >>> person.write_attribute :name, "Bilal" > => "Bilal" >>> person.read_attribute :name > "read_override" > => "Bilal" That's very interesting. I have a hunch it must have to do with autoloading... it has the flavor of what would happen if the original file were re-loaded after the redefinition had already taken place but before write_attribute is called. I don't know why that would be, and I don't think I've nailed it, but I'll keep looking. David -- David A. Black, Director Ruby Power and Light, LLC (http://www.rubypal.com) Ruby/Rails training, consulting, mentoring, code review Book: The Well-Grounded Rubyist (http://www.manning.com/black2) --1926193751-1692545108-1253760909=:30054-- --1926193751-1692545108-1253760909=:30054--