nobu.nokada
11/15/2004 12:22:00 AM
Hi,
At Mon, 15 Nov 2004 06:37:11 +0900,
trans. (T. Onoma) wrote in [ruby-talk:120320]:
> I am doing something very similar with a markup parser (made-up example):
>
> tr = token_registry {
> block :literal, '"""', '"""'
> line :strong, '*', '*'
> line :italic, '_', '_'
> }
>
> One of things that made the methods even better was not having to worry about
> the namespace for the classes themselves --in other words, the classes Block
> and Line themselves are, in a way, private.
What about this way? Though I don't think the name
class_method is proper...
module ClassMethods
def class_method(name, superclass = ::Object, method = name.to_s.downcase,
&definition)
c = Class.new(superclass, &definition)
define_method(method, &c.method(:new))
module_function method
const_set(name, c)
end
end
if $0 == __FILE__
module M
extend ClassMethods
class_method(:A) do
def initialize ; puts "A" ; end
end
class_method(:B) do
def initialize ; puts "B" ; end
end
end
p M.a, M.b
end
--
Nobu Nakada