Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby:模块问题_Ruby - Fatal编程技术网

Ruby:模块问题

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

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=>

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给出了完整的答案。