初学ruby 怎么把一个类导入呢
1. 例如在
Person.rb 中
- Python code
class Person def talk puts "nihao" end end
Student.rb
- Python code
class Student < Person def talk puts "nihao" end endp = Student.newp.talk
结果
>ruby Student.rb
Student.rb:1: uninitialized constant Person (NameError)
>Exit code: 1
[解决办法]
哈,是Python还是Ruby啊?
ruby不会自动到当前路径去找类的,要自己加。
require 'Person'
[解决办法]
自己添加require上去就行了~~~不过发现csdn ruby on rails 貌似人不多阿~~
[解决办法]
不用那么麻烦的,在ruby中,include就可以实现继承
- Python code
class Student include Person def talk puts "nihao" end end