Conditional class extension

T

Trans

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.
 
J

Joel VanderWerf

Trans said:
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).
 
F

Florian Frank

Trans said:
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.
if defined? Foo::Bar
# ...
end
 
R

Ross Bamford

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.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top