From: Brian Candler on
Roberto Cm wrote:
> -- The problem with the save method is .. well actually nothing, I need
> that too :) But I want to not have to explicitly call save, and instead
> have the object detect changes and behave dynamically -- ie, assignment
> to the config instance should write directly to disk as well as to the
> attribute during the set_x methods.
> But how do I change setter methods that don't exist yet?

I don't see what you mean. You are defining the []= method yourself, so
you can make it do whatever you like, including call 'save'. It doesn't
matter if you define the []= method first, or the save method first.
What matters is whether the method exists *at the time it is called*.

If you are subclassing you can use super:

class AutoSaveConfig < ConfigYAML
def []=(*args)

If you are monkey-patching (overriding code in an existing class without
modifying the original source file) you can use alias:

class ConfigYAML
alias :old_set :[]=
def []=(*args)

Note: the class name "Config" (in the original code) is not a good
choice, because Ruby has its own Config class, commonly used when
building extensions.

require 'rbconfig'
puts Config::CONFIG['libdir']
Posted via