0
我正在嘗試使用self.instance_exec
方法。在我的案件實例變量@legend
會打印非常漂亮,但類變量拋出一個錯誤:在匿名函數中訪問全局類變量
uninitialized class variable @@holiday_legend_counter in Object (NameError)
我的示例代碼:
class Calender
def initialize(options)
@@holiday_legend_counter = "a"
@legend = 'A'
end
def print_date(print_date)
# some calculation to calculate date and the current date
self.instance_exec date, @current_start_date, &print_date
end
end
print_legend = Proc.new do |date,current_date|
print @@holiday_legend_counter
print @legend
end
cal = Calender.new
cal.print_date(print_legend)