Ruby:模块问题
cat模块1.rb:Ruby:模块问题,ruby,Ruby,cat模块1.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) ruby模块1.rb=> modul
#!/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)
ruby模块1.rb=>
module1.rb:13: syntax error, unexpected keyword_end, expecting $end
有人能修好它吗?你需要一个小写的m来启动它 哦,模块名称应该是一个常量 从
module Module1
module关键字区分大小写,正如Ray所说,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)
我把它从一个模块改为另一个模块,module1.rb:3:类/模块名必须是常量,我已经在我的答案中添加了它。vderyagin给出了完整的答案。