鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 编程语言开发 > ruby > >

ruby 语法(转)

来源:互联网 作者:佚名 时间:2013-05-15 07:28
转自 1. if语句 if语句是示例如下: [ruby] #encoding:gbk FAN = 10; if FAN5 puts "FAN 5"; elsif FAN3 puts "3 FAN 5"; else puts "FAN 3"; end #encoding:gbk FAN = 10; if FAN5 puts "FAN 5"; elsif FAN3 puts "3 FAN 5"; else puts "FAN 3"; end 2. unl

转自

1. if语句


if语句是示例如下:


[ruby]
#encoding:gbk  
FAN = 10; 
if FAN>5 
    puts "FAN > 5"; 
elsif FAN>3 
    puts "3 < FAN < 5"; 
else 
    puts "FAN < 3"; 
end 

#encoding:gbk
FAN = 10;
if FAN>5
 puts "FAN > 5";
elsif FAN>3
 puts "3 < FAN < 5";
else
 puts "FAN < 3";
end

2. unless语句


unless语句和if相反,虚拟主机,如果条件不成立则执行


[ruby]
#encoding:gbk  
FAN = 10; 
if FAN>5 
    puts "FAN > 5"; 
elsif FAN>3 
    puts "3 < FAN < 5"; 
else 
    puts "FAN < 3"; 
end 
 
puts "*******************************"; 
 
puts "unless test" unless FAN > 20; 

#encoding:gbk
FAN = 10;
if FAN>5
 puts "FAN > 5";
elsif FAN>3
 puts "3 < FAN < 5";
else
 puts "FAN < 3";
end

puts "*******************************";

puts "unless test" unless FAN > 20;

3. case语句
[ruby]
#encoding:gbk  
FAN = 10; 
if FAN>5 
    puts "FAN > 5"; 
elsif FAN>3 
    puts "3 < FAN < 5"; 
else 
    puts "FAN < 3"; 
end 
 
puts "*******************************"; 
 
puts "unless test" unless FAN > 20; 
 
puts "*******************************"; 
 
NUM = 6; 
case NUM 
    when 5 
        puts "5"; 
    when 6 
        puts "6"; 
end 

#encoding:gbk
FAN = 10;
if FAN>5
 puts "FAN > 5";
elsif FAN>3
 puts "3 < FAN < 5";
else
 puts "FAN < 3";
end

puts "*******************************";

puts "unless test" unless FAN > 20;

puts "*******************************";

NUM = 6;
case NUM
 when 5
  puts "5";
 when 6
  puts "6";
end
case语句中的when 还可以是一个范围。


4. while循环
[ruby]
#encoding:gbk  
FAN = 10; 
a = 1; 
 
while a < FAN do  
    puts a; 
    a=a+1; 
end 

#encoding:gbk
FAN = 10;
a = 1;

while a < FAN do
 puts a;
 a=a+1;
end
Ruby中不支持++运算符


5. for循环
[ruby]
#encoding:gbk  
FAN = 10; 
a = 1; 
 
while a < FAN do  
    puts a; 
    a=a+1; 
end 
 
FOODS = ["巧克力","面包","蛋糕","牛奶"]; 
for f in FOODS do  
    puts f; 
end 

#encoding:gbk
FAN = 10;
a = 1;

while a < FAN do
 puts a;
 a=a+1;
end

FOODS = ["巧克力","面包","蛋糕","牛奶"];
for f in FOODS do
 puts f;
end


还可以遍历Hash对象


[ruby]
#encoding:gbk  
FAN = 10; 
a = 1; 
 
while a < FAN do  
    puts a; 
    a=a+1; 
end 
 
FOODS = ["巧克力","面包","蛋糕","牛奶"]; 
for f in FOODS do  
    puts f; 
end 
 
Stus = {"小王"=>"29","小李"=>"23","小宋"=>"25"}; 
 
for name,age in Stus do  
    puts name, age; 
end 

#encoding:gbk
FAN = 10;
a = 1;

while a < FAN do
 puts a;
 a=a+1;
end

FOODS = ["巧克力","面包","蛋糕","牛奶"];
for f in FOODS do
 puts f;
end

Stus = {"小王"=>"29","小李"=>"23","小宋"=>"25"};

for name,age in Stus do
 puts name, age;
end

6. 迭代器


使用数组或者Hash对象自带的迭代器


[ruby]
#encoding:gbk  
FAN = 10; 
a = 1; 
 
 
 
FOODS = ["巧克力","面包","蛋糕","牛奶"]; 
FOODS.each do |f| 
    puts f; 
end 
 
Stus = {"小王"=>"29","小李"=>"23","小宋"=>"25"}; 
Stus.each do |name,age| 
    puts name,age; 
end 

#encoding:gbk
FAN = 10;
a = 1;

 

FOODS = ["巧克力","面包","蛋糕","牛奶"];
FOODS.each do |f|
 puts f;
end

Stus = {"小王"=>"29","小李"=>"23","小宋"=>"25"};
Stus.each do |name,age|
 puts name,age;
end

7. 循环控制

网友评论
<