2011-07-25 261 views
0

猫module1.rb:红宝石:模块问题

#!/home/user1/.rvm/rubies/ruby-1.9.2-p180/bin/ruby 

Module module1 

    def add(a,b) 
     return a+b 
    end 

    def subtract(a,b) 
     return a-b 
    end 

end 


temp = "nothing" 
temp.extend module1 
temp.add(5,2) 

红宝石module1.rb =>

module1.rb:13: syntax error, unexpected keyword_end, expecting $end 

谁能解决这个问题?

回答

1

你需要一个小写字母m来启动它。

Oh和模块名应该是恒定....

开始与

module Module1 
+0

我改变了它从模块到模块,module1.rb:3:类/模块的名称必须是恒定的 – newcomer

+0

是的,我补充说,我的答案了。 vderyagin给出了完整的答案。 –

11

module关键字是大小写敏感的,并且,作为雷说,模块必须是常数(恒定Ruby中的名称以大写字母开头)。这工作:

module Module1 

    def add(a,b) 
     return a+b 
    end 

    def subtract(a,b) 
     return a-b 
    end 

end 


temp = "nothing" 
temp.extend Module1 
temp.add(5,2)