From: Robert Klemme Date: 2004-02-28T07:29:45+09:00 Subject: Re: Extensible meta-data ? "Its Me" schrieb im Newsbeitrag news:l9L%b.8114$lS1.3171@fe2.texas.rr.com... > > "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 Even nicer: if Hash === args[0] > > 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. I tried a bit with instance_method but didn't get the desired results so I omitted that part. Now I know what's wrong here: You can't use UnboundMethod as hash key because the hash value is not dependent on the class and method identifier but on the id of the UnboundMethod object only: irb(main):001:0> class Foo; def bar;end; end => nil irb(main):002:0> Foo.instance_method( :bar ) => # irb(main):003:0> a = Foo.instance_method( :bar ) => # irb(main):004:0> b = Foo.instance_method( :bar ) => # irb(main):005:0> a == b => true irb(main):006:0> b == a => true irb(main):007:0> a.hash == b.hash => false irb(main):008:0> a.id => 135052720 irb(main):009:0> a.hash => 135052720 irb(main):010:0> b.id => 135044800 irb(main):011:0> b.hash => 135044800 UnboundMethod#hash is broken in $ ruby --version ruby 1.8.1 (2003-12-25) [i386-cygwin] > Thanks for the code; always amazes me how others' experience shows in much > more efficient coding styles than my own :) Thanks! It's always fun to see what differnt solutions are possible with Ruby. Kind regards robert