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

相关文章:

  • 2022-02-07
  • 2021-07-02
  • 2022-12-23
  • 2021-12-24
  • 2021-11-21
  • 2021-05-15
  • 2021-07-20
  • 2021-12-11
猜你喜欢
  • 2022-12-23
  • 2021-07-04
  • 2021-06-09
  • 2021-08-16
  • 2021-06-04
  • 2021-11-21
  • 2021-04-06
相关资源
相似解决方案