Ruby on Rails 入门之:(7) Ruby中表达式、循环、遍历
1. 表达式
Ruby中表达式分为有返回值和没有返回值的。
if语句是有返回值的,while语句是没有返回值的,表达式的返回值是最后执行的那一行的值。
有返回值的表达式:
[ruby]
#encoding:gbk
score = 569;
INFO = "";
result = if score>600
puts "score > 600";
INFO = "score > 600";
else
puts "score < 600";
INFO = "score < 600";
end
puts result;
#encoding:gbk
score = 569;
INFO = "";
result = if score>600
puts "score > 600";
INFO = "score > 600";
else
puts "score < 600";
INFO = "score < 600";
end
puts result;
输出结果:
[html]
watkins@watkins:~/temp/workspace/ruby$ ruby expression.rb
score < 600
expression.rb:9: warning: already initialized constant INFO
score < 600
watkins@watkins:~/temp/workspace/ruby$
watkins@watkins:~/temp/workspace/ruby$ ruby expression.rb
score < 600
expression.rb:9: warning: already initialized constant INFO
score < 600
watkins@watkins:~/temp/workspace/ruby$
2. 并行赋值,嵌套赋值
Ruby语言支持并行赋值和嵌套复制,比其他的语言方便的的多。
2.1 并行赋值
在Ruby中,通过并行赋值,可以很方便的交换两个变量的值,而不像其他的语言需要使用第三个变量。
[ruby]
#encoding:gbk
name1, name2 = "xiaowang", "zhaohong";
puts name1, name2;
# 交换变量
name1, name2 = name2, name1;
puts name1, name2;
#encoding:gbk
name1, name2 = "xiaowang", "zhaohong";
puts name1, name2;
# 交换变量
name1, name2 = name2, name1;
puts name1, name2;
输出的结果为:
[html]
watkins@watkins:~/temp/workspace/ruby$ ruby expression.rb
xiaowang
zhaohong
zhaohong
xiaowang
watkins@watkins:~/temp/workspace/ruby$
watkins@watkins:~/temp/workspace/ruby$ ruby expression.rb
xiaowang
zhaohong
zhaohong
xiaowang
watkins@watkins:~/temp/workspace/ruby$
并行赋值中*号的使用:
在并行赋值中,左边的变量以“*”作为前缀,就表情所有对应该变量的值以及后面的值会被作为一个数组,并赋值给这个变量。
如果右边的值以“*”作为前缀,则会将这个数组拆开,按照元素的相应位置进行赋值。
看一个例子:
[ruby]
#encoding:gbk
seasons = ["spring","summer","fall","winter"];
tempertures = ["spring:18","summer:38","fall:15","wintern:-2"];
a,b = seasons;
puts a,b;
a, *b = seasons;
puts a;
puts "b is for:";
puts b;
puts "***********************"
c,d, e = "hello", *tempertures;
puts c,d,e;
#encoding:gbk
seasons = ["spring","summer","fall","winter"];
tempertures = ["spring:18","summer:38","fall:15","wintern:-2"];
a,b = seasons;
puts a,b;
a, *b = seasons;
puts a;
puts "b is for:";
puts b;
puts "***********************"
c,d, e = "hello", *tempertures;
puts c,d,e;
输出如下:
[html]
watkins@watkins:~/temp/workspace/ruby$ ruby expression.rb
spring
summer
spring
b is for:
summer
fall
winter
***********************
hello
spring:18
summer:38
watkins@watkins:~/temp/workspace/ruby$
watkins@watkins:~/temp/workspace/ruby$ ruby expression.rb
spring
summer
spring
b is for:
summer
fall
winter
***********************
hello
spring:18
summer:38
watkins@watkins:~/temp/workspace/ruby$
2.2 嵌套赋值
嵌套赋值,可以让左边的变量用括号扩起来,形成变量列表。
[ruby]
#encoding:gbk
seasons = ["spring","summer","fall","winter"];
tempertures = ["spring:18","summer:38","fall:15","wintern:-2"];
=begin
a,b = seasons;
puts a,b;
a, *b = seasons;
puts a;
puts "b is for:";
puts b;
puts "***********************"
c,d, e = "hello", *tempertures;
puts c,d,e;
=end
b,(c,d),e = seasons;
puts b,c,d,e;
puts "*********************************";
seasons_ext = ["spring",["summer","middle","fall"],"winter"];
b, (c, *d),e=seasons_ext;
puts b,c,d,e;
puts "*********************************";
puts d;
#encoding:gbk
seasons = ["spring","summer","fall","winter"];
tempertures = ["spring:18","summer:38","fall:15","wintern:-2"];
=begin
a,b = seasons;
puts a,b;
a, *b = seasons;
puts a;
puts "b is for:";
puts b;
puts "***********************"
c,d, e = "hello", *tempertures;
puts c,d,e;
=end
b,(c,d),e = seasons;
puts b,c,d,e;
puts "*********************************";
seasons_ext = ["spring",["summer","middle","fall"],"winter"];
b, (c, *d),e=seasons_ext;
puts b,c,d,e;
puts "*********************************";
puts d;
输出的结果:
[html]
watkins@watkins:~/temp/workspace/ruby$ ruby expression.rb
spring
summer
fall
*********************************
spring
summer
middle
fall
winter
*********************************
middle
fall
watkins@watkins:~/temp/workspace/ruby$
watkins@watkins:~/temp/workspace/ruby$ ruby expression.rb
spring
summer
fall
*********************************
spring
summer
middle
fall
winter
*********************************
middle
fall
watkins@watkins:~/temp/workspace/ruby$
第一次输出中d为空,因为(c,d)对应数组中的第二项,然后嵌套的c对应第二项中的第一个,那么d就不存在了。
*号的使用和上面的一样。
补充:Web开发 , 其他 ,