首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > Ruby Rails >

初学ruby 如何把一个类导入呢

2012-03-03 
初学ruby 怎么把一个类导入呢1. 例如在Person.rb 中Python codeclass Persondef talkputs nihaoendendSt

初学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 

热点排行