Reputation: 2921
I came to ruby from PHP. How could i do the next thing in ruby?
$className = 'ArrayObject';
$arrayObject = new $className();
Upvotes: 25
Views: 11250
Reputation: 3324
You can also use the following if you are using Ruby on Rails:
array_object = "Array".constantize.new
Upvotes: 24
Reputation: 96997
If you have a class, like for example String:
a = String
a.new("Geo")
would give you a string. The same thing applies to other classes ( number & type of parameters will differ of course ).
Upvotes: 6
Reputation: 115432
You can do this:
arrayObject = Object::const_get('Array').new
Upvotes: 33