概述红宝石设计问题
我写一个使用数据从MySQL查询创建图表URL的Ruby程序。最近出现了一个新的要求,我们可能需要在未来创建带分组条形图的图形。因此,我可以有任意数量的数据而不是有一组数据。现在,我的BarChart对象的构造函数只需要一个单一的数据数组,而我正在寻找类似于Ruby的方法来允许多个数据数组。
电流构建
#constructor
#title The title of the graph
#data The data that will go in the bar chart
#labels The labels that match the data
#x_axis_label The label for the x axis
#y_axis_label The label for the y axis
def initialize(title, data, labels, x_axis_label, y_axis_label)
@title, @data1, @labels, @x_axis_label, @y_axis_label =
title, data, labels, x_axis_label, y_axis_label
super(@title, @@type, @@size)
@url = to_url()
end
我尝试
我最初的想法是使用变参。
#constructor
#title The title of the graph
#data The data that will go in the bar chart
#labels The labels that match the data
#x_axis_label The label for the x axis
#y_axis_label The label for the y axis
def initialize(title, *data, labels, x_axis_label, y_axis_label)
.....
end
这是个不错的点子吗?还是有更好的方法去解决它?
谢谢
合并!您用默认值覆盖参数。您可以通过以下方式对其进行测试: p({:a => 1} .merge!({:a => 2})) – knut
哎呀。我打算使用ActiveSupport中的'reverse_merge!'(如果不使用它,很容易实现)。但我会解决它。 –