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

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,616
Members
45,306
Latest member
TeddyWeath

Latest Threads

Top