--45Z9DzgjV8m4Oswq
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Wed, Oct 05, 2005 at 05:45:49AM +0900, Louis J Scoras wrote:
> So what would be the best way to do this if the data needs to be read-write?
...
> But what would you do if the classes in question weren't meant to be
> singletons? 

In that case the data must ultimately be stored in the individual
instances.  You can copy it there at #initialize time.  Check out
_why's Creature class at the bottom of this page:

http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html

> Trying to use module_eval to dynamically define a class method doesn't seem
> to work, because it's not available in the constructor.

You can dynamically define class methods like this:

class Metaprogrammable
  def self.add_feature(f)
    meta = class << self; self; end
    meta.instance_eval {
      define_method(f) do
        "This is class method #{f}"
      end
    }
  end
end

class Foo < Metaprogrammable
  add_feature :bogus
end

Remember that a Class is also an Object.  When you create class
methods, you're extending one particular instance of the class
"Class".  So this:

class << MyClass
  def a_class_method
    ...
  end
end

is equivalent to this:

def MyClass.a_class_method
 ...
end

and also this:

class MyClass
  def self.a_class_method
    ...
  end
end

regards,
Ed

--45Z9DzgjV8m4Oswq
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDQvE7nhUz11p9MSARAkmfAKDCzRPhLJ2E7D7Qr1Cql7wBvg7EFQCgzxzt
cENHN5h8qozkA+iVVQg2zV031
-----END PGP SIGNATURE-----

--45Z9DzgjV8m4Oswq--