Assuming I can construct a string that matches an existing class, how do I invoke it?
For example, I have several classes:
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
constantize
fits the bill. You can read more about it here. In your case it would be something like:
(1..3).each do |n|
"MyClass#{n}".constantize.methods
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With