ruby class/module name must be CONSTANT 错误

 Ruby 错误提示: 

class/module name must be CONSTANT

 =begin begin to comment

this  is  also comment
= end

puts 
" noLonggerComments " ;

# rdoc rules
#
= headers
#
this is header content
#
== sub headers
#
 this is sub header content
#
=== sub sub headers
#
 this is sub sub headers
class  animal
 
# this is the comment for the  'new' method !
  def  initialize type
    @name 
=  type
 end
 
 
# this is comment for 'shout' method 
  #  Second Line
  # * this is sub line 1
  # * this is sub line 2
 
 
def  shout
    
if  @name  ==   ' dog '
        puts 
" wang! wang! "
    elsif @name 
==   ' cat '
        puts 
" miao! miao! "     
    end    
 end
 
end

dog 
=  animal.new  ' dog '
dog.shout

gets

 Ruby 类名 必须大写。 

这里涉及到一个Ruby 解释器的习惯约定。

:: 

1.把以ACSII编码的且为大写开头的变量 默认为 常量。

2. Ruby中的局部变量名首字母的约定是小写。

3.Ruby中的构造函数名称为initialize。

4.Ruby中的成员变量(实例变量)前导@符,在initialize里进行声明与初始化。
5.Ruby中的属性用attr_writer和attr_reader声明,分别对应c#的set,get,使用了attr_accessor是可读也可写
6.Ruby中的全局变量前导$符。
7.Ruby中的常数(常量)用大写字母开头,约定是全部大写。

解决方案:

将类名改成 Animal 之后就可以通过。 

转载于:https://www.cnblogs.com/ToDoToTry/archive/2011/07/24/2115325.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值