How do I dynamically invoke or call a class in Rails?

Viewed 6905

Assuming I can construct a string that matches an existing class, how do I invoke it?

For example, I have several classes:

  • MyClass1
  • MyClass2
  • MyClass3

And I want to dynamically invoke each of them by constructing a string that matches their names. If they all had the method "methods", how do I do something like this?:

(1..3).each do |n|
  ("MyClass"+n).methods
end
2 Answers
Related