Mario Stoilov Mario Stoilov - 3 months ago 7
Ruby Question

Template methods in ruby

Simple question:

In java you can define :

void myFunction<T>(T value) { //do stuff }

Is there an equivalent in ruby, and if not, how can I achieve a similar result (passing class types?)


You can pass a class to a method just like passing normal objects. For example

def create_object(klass, *args)*args)

create_object(String)  #=> ""
create_object(Hash)  #=> {}
create_object(Array, 3, :hello)  #=> [:hello, :hello, :hello]