I have a Mixin like this:
Module B
def B.append_features(aClass)
def aClass.foo
“foo”
end
end
end
o = Object.new
o.instance_eval<<-“end_eval”
include B
end_eval
But o.foo does not exist!?
Is there a way to use append_features together with class<<self ?
See Bob’s answer – but also, I’m wondering whether there might be
simpler ways to do what you want to do.
[…]
Do you actually need #append_features for what you want to do? (Not a
rhetorical question – it is at least as likely that I am missing the
point as that my simplifications are appropriate 
To be honest I don’t remember the exact reason why I uses
append_features beside it was the only thing that worked out.
But I will try to give you a overview of the things I’m doing. As I did
not found any persistance framework for ruby I’m currently developing
one for my own. One of the most important features to me is that the
Object that should persisted can be easily declared. It came to my mind
to use a way like the attr_read function: attr_persist. This is declared
in a Mixim Module which simply declares a function that call a
Persistors write_attr or read_attr method:
Code extract:
module Persist
require ‘singleton’
class HashPersistor
include Singleton
include Enumerable
@allvalues = {}
def write(obj, attr, value)
@allvalues = {} unless @allvalues != nil
@allvalues[obj] = {} unless @allvalues.has_key? obj
@allvalues[obj][attr] = value
end
def read(obj, attr)
@allvalues[obj][attr]
end
def each
@allvalues.keys
end
end
def Persist.append_features(includingClass)
super
def includingClass.persist(*ids)
for id in ids
ev=<<-“end_eval”
attr_accessor :persistor
@@persistor = Persist::HashPersistor.instance
def #{id.id2name}(*args, &block)
persistor.read(self, "#{id.id2name}")
end
def #{id.id2name}=(*args, &block)
persistor.write(self, "#{id.id2name}", *args)
end
end_eval
end
ev+=<<-"end_eval"
def find_all
persistor.each.to_a
end
end_eval
module_eval ev
end
end
end
end
I came to a problem when I’d implemented a dbiPersistor when I used to
restore an instance from the database. I need to declare all the
accessors to the persisted class fields at runtime. Sth. like the
following did not work:
o = Object.new
o.instance_eval<<-“env_eval”
class << self
include Persist
end
end_val
attrs.each(|attr| o.instance_eval(“persist :${attr}”));
I hope this gave you some insight in my motivation.
-billy.
···
On Sat, Jun 22, 2002 at 04:23:24AM +0900, David Alan Black wrote:
–
Meisterbohne Söflinger Straße 100 Tel: +49-731-399 499-0
eLösungen 89077 Ulm Fax: +49-731-399 499-9