Более идиоматический способ добиться этого в Ruby:
module B
def self.included(klass)
klass.class_eval <<-ruby_eval
CONST = find
ruby_eval
# note that the block form of class_eval won't work
# because you can't assign a constant inside a method
end
end
class A
def self.find
"AAA"
end
include B
end
puts A::CONST
То, что вы делали (класс << base), фактически помещает вас в контекст <code>metaclass А, а не самого А. Метод find
находится на самом A, а не на его метаклассе. Следует помнить, что сами классы являются объектами и поэтому имеют свои собственные метаклассы.
Чтобы попытаться прояснить ситуацию:
class Human
def parent
# this method is on the Human class and available
# to all instances of Human.
end
class << self
def build
# this method is on the Human metaclass, and
# available to its instance, Human itself.
end
# the "self" here is Human's metaclass, so build
# cannot be called.
end
def self.build
# exactly the same as the above
end
build # the "self" here is Human itself, so build can
# be called
end
Не уверен, поможет ли это, но если вы этого не понимаете, вы все равно можете использовать идиому class_eval выше.