On 18/08/2011, at 3:19 PM, Samuel Richardson wrote:

> Dammit Paul, just as I was typing this out ;)
> 
> There is a limitation in Pauls method though with nested Classes/Modules. If
> they are nested you can use something like:
> 
> # Get a class based on a string, handles :: module nesting
> def self.class_from_string(str)
> str.split('::').inject(Object) do |mod, class_name|
> mod.const_get(class_name)
> end
> end

Yep, this is what I've used in the past.

I'd just like to add, although I agree with all this advice, if you're using 
const_get I'd say there's more than a 50% chance that you'd be better off 
designing your code in a way that doesn't need it.

Metaprogramming is sometimes the right tool for the job, but:

- it usually isn't,
- it's rarely easily maintainable/testable, and most importantly,
- it usually violates the principle of least surprise.

Maybe you can define a BaseClass.for builder that can decide which subclass to 
build with, based on whatever conditions?

-Ben

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.

Reply via email to