From: Phrogz Date: 2007-11-09T05:20:03+09:00 Subject: Re: standard, pretty, and flexible meta-methods? On Nov 8, 12:57 pm, Greg Weeks wrote: > Is there a way to make meta-methods like "def_foo" available in class > definitions without using inheritance? Is there a standard way? Is it > pretty? (Or is it perhaps pointless?) In addition to using modules to mix in methods to only specific classes, I should point out that you can define methods in the Module class to have them available to every class and module you create. (Or you can define them in the Class class to have them available only to classes but not modules.) class Module def make_method( name ) define_method( name ){ "Hello from #{name}" } end end class Foo; end Foo.make_method( 'jimmy' ) p Foo.new.jimmy #=> "Hello from jimmy" module Whee; end class Foo include Whee end Whee.make_method( 'junk' ) p Foo.new.junk #=> "Hello from junk" class Class def classes_only! "Classes are awesome. Modules suck!" end end p Foo.classes_only! #=> "Classes are awesome. Modules suck!" p Whee.classes_only! #=> undefined method `classes_only!' for Whee:Module (NoMethodError)