我在Ruby类中定义了一堆类变量并将它们初始化为一个值,但是当我在to_url
函数中打印它们时它们都是null,有人可以告诉我为什么???
class BarChart < GoogleChart
@axes_prefix = "chxt="
@data_scale_prefix = "chds="
@type = "#@@type_prefix" + "bvg"
@size = "#@@size_prefix" + "800x375"
@colors = "#@@color_prefix" + "4466AA"
@axes = "#@axes_prefix" + "x,y,x,y"
@x_axis_index = "0"
@y_axis_index = "1"
@x_axis_label_index = "2"
@y_axis_label_index = "3"
@axes_label_position_prefix = "chxp="
@axis_range_prefix = "chxr="
def initialize(title, data, labels, x_axis_label, y_axis_label)
@title, @data, @labels, @x_axis_label, @y_axis_label =
title, data, labels, x_axis_label, y_axis_label
super(@title, @type, @size)
to_url()
end
def to_url()
puts @axes_prefix, @data_scale_prefix, @type, @size, @colors,
@axes, @x_axis_label_index, @y_axis_label_index, @x_axis_index, @y_axis_index
end
def start()
b = BarChart.new("CHART", "0,1,2,3,4,5,6,7,8,9",
"ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE",
"Zero to Nine", "Numbers").to_s()
end
if __FILE__ == $0
start()
end
任何想法都会很棒。
感谢。
答案 0 :(得分:3)
@@foo
- 它的类变量)initialize
方法。答案 1 :(得分:2)
您直接在类中定义变量,这意味着它们属于类本身,而不属于它的实例。您可以直接从类方法访问它们,但不能从实例方法(这是您的to_url)中访问它们。如果您希望可以从实例方法访问它们 - 在另一个实例方法中定义该变量,例如,在initialize
中。