Conditional class extension

Is there a better way to do conditional class extension?

module Foo
if const_get(:Bar)
class Bar
def x; end
end
end recue nil
end

The ‘if const_get … rescue nil’ doesn’t seem very elegant.

Thanks,
T.

Trans wrote:

Is there a better way to do conditional class extension?

module Foo
if const_get(:Bar)
if defined? Bar

That works if you are referring literally to Bar, but not if you just
have a variable referring to a string that might or might not be defined
as a constant in Foo. In that case, you could use
constants.include?(str).

On Mon, 2006-10-09 at 06:55 +0900, Trans wrote:

The ‘if const_get … rescue nil’ doesn’t seem very elegant.
Just a small change, but why not just ask if const_defined? ?

Trans wrote:

The ‘if const_get … rescue nil’ doesn’t seem very elegant.

if defined? Foo::Bar

end

Ross B. wrote:

The ‘if const_get … rescue nil’ doesn’t seem very elegant.

Just a small change, but why not just ask if const_defined? ?

Oh yea! :slight_smile:

Thanks,
T.