R
Ryan Hinton
I am working on a nice little document-based app where I would like to
take selective action based on whether data have been modified (e.g.
prompt to save before exit). When I imagine writing a bunch of
"memberVariable=(newVar)" methods, all with the same pattern for the
guts, I think there must be a better way.
How does attr_writer do this? I tried to figure out how to do
something similar in vanilla Ruby, but I don't see a way to
programmatically define a new method. Would I have to write a C
extension for this?
Here is roughly what I want.
module AttrWriterTrackModified
def attr_writer(*syms)
syms.each do |sym|
eval("def #{sym}=(val)
if @#{sym} != val
@#{sym} = val
@modified = true
end
end;")
end
end
end
take selective action based on whether data have been modified (e.g.
prompt to save before exit). When I imagine writing a bunch of
"memberVariable=(newVar)" methods, all with the same pattern for the
guts, I think there must be a better way.
How does attr_writer do this? I tried to figure out how to do
something similar in vanilla Ruby, but I don't see a way to
programmatically define a new method. Would I have to write a C
extension for this?
Here is roughly what I want.
module AttrWriterTrackModified
def attr_writer(*syms)
syms.each do |sym|
eval("def #{sym}=(val)
if @#{sym} != val
@#{sym} = val
@modified = true
end
end;")
end
end
end