红宝石中的模板方法 [英] Template methods in ruby
本文介绍了红宝石中的模板方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个简单的问题:
在Java中,您可以定义:
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)
klass.new(*args)
end
create_object(String) #=> ""
create_object(Hash) #=> {}
create_object(Array, 3, :hello) #=> [:hello, :hello, :hello]
这篇关于红宝石中的模板方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文