From: Its Me Date: 2004-02-28T02:34:47+09:00 Subject: Re: Extensible meta-data ? "Robert Klemme" wrote... > > > And then use this: > > class Mine; end meta ... # metadata on Mine > > I don't think this will work since class...end doesn't return the class. Correct. I know I could stick in a "self" before the end to make it work, but am very curious why it does not return the class. Similarly, I am very curious why def...end does not return a useful object (UnboundMethod? Proc? I'm not a guru, but "nil" is not an intuitive return to me). > > class Mine > > def foo; end meta ... # metadata on Mine#foo > > end > > > > Thoughts? Am I trying to do something ridiculous? > > How about this: > > class Module > def meta(*args) > case args.size > when 0 > meta_data[self] > when 1 > if args[0].kind_of? Hash > meta_data[self].update args[0] > else > meta_data[args[0]] > end > when 2 > meta_data[args[0]].update args[1] > else > raise ArgumentError, "Wrong number of args" > end > end > > def meta_data > @meta ||= Hash.new {|h,k| h[k] = {}} > end > end > > class Foo > def foo; puts "bar"; end > meta :foo, "method" => "metadata", "foo" => "bar" > meta "class" => "metadata", "foo" => "bar" > end > > p Foo.meta :foo > p Foo.meta I really want to attach Method meta-data to the Method, not to the class. If I can't figure out how to get a handle on the method object, then perhaps I'll do something like this. Thanks for the code; always amazes me how others' experience shows in much more efficient coding styles than my own :)