On Mar 25, 2009, at 2:17 PM, Andrew S. Townley wrote: > Thanks. > > If you had to register the namespaces first in some kind of tagz > registry, maybe you could do something by handling the NameError when > you did: > > foo.bar_('bar:foo' => 42) > > If it had registered a namespace prefix of foo, then it'd just "do the > right thing", otherwise, you'd get the normal name error. This would > probably be the most logical syntax, but it's way beyond my Ruby > magical > abilities to figure out how to actually make it work. > > Still, I think it'd be the most readable of the proposed approaches... > I don't want to have to find unicode characters on my keyboard! :) > > Cheers, how about this cfp:~ > cat a.rb require 'rubygems' require 'tagz' class Namespace def initialize &block @ns = block.call.to_s @context = block @self = eval 'self', block end instance_methods.each{|m| undef_method if m[%r/__/]} def method_missing m, *a, &b if m.to_s[%r/^_|_$/] @self.send("#{ @ns }:#{ m }", *a, &b) else "#{ @ns }:#{ m }" end end end include Tagz.globally a = Namespace.new{ :a } b = Namespace.new{ :b } xml = a.root_{ a.foo_(b.bar => 'bar'){ 'content' } b.bar_(a.foo => 'foo'){ 'content' } } puts xml cfp:~ > ruby a.rb <a:root><a:foo b:bar="bar">content</a:foo><b:bar a:foo="foo">content</ b:bar></a:root> a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama