我有以下模块,其中有一个变量,其中包含一个字符串的假设年的第一天,1个方法输出一个字符串和另一种方法,还输出一个字符串:
模块和模块访问变量(Ruby语言)
module Week
first_day = "Sunday"
def weeks_in_month
puts "There are 4 weeks in a month"
end
def weeks_in_year
puts "There are 52 weeks in a year"
end
end
我现在有一类是谁的唯一目的是打印出位于模块中的变量。(这只是用于测试目的)
class Decade
include Week
def firstday
puts Week::first_day
end
end
我现在实例十年和访问方法位于模块中使用数十年的对象。我的程序调用firstday方法时运行到一个问题
z = Decade.new
z.weeks_in_month
z.weeks_in_year
z.firstday #Errors here
我得到的错误是:
undefined method `first_day' for Week:Module (NoMethodError)
我是新来的Ruby和我只是习惯了模块,所以任何帮助将是赞赏。
不要忘记用'{}'按钮或四个缩进空格来格式化代码。 – tadman