Ruby语言学习记录

1、不需要声明即可使用变量

2、类的名称的第一字母必须大写

3、一个有构造函数的典型类

class Rectangle

attr_accessor:height,:width

def initialize (hgt,wdth)

@height = hgt

@width = wdth

end

def area ()

@height*@width

end

end

4、程序控制

(1)循环控制

i=0

while i<10

puts "OK"

i=i+1

end

(2)#遍历对象集合

for element in [2,9.8,"some string", math::pi]

print "the type is: " + element.type.to_s + "\n&"

end

(3)判断语句

if area > 100

"big"

else

"small"

end

(4)类似case的控制

case height

when 1

print "stubby\n"

when 2..10 #高度范围为2~10

print "short\n"

when 10..20 #高度范围为2~10

print "tall\n"

end