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 之后就可以通过。