Ruby语言基础学习四:Ruby 条件、循环

#_*_ coding:utf-8 _*_

x=1
if x>2
	puts "x bigger than 2"
elsif x<=2 and x!=0
	puts "x is 1"
else
	puts "can not get the value of x."
end

# 通常我们省略保留字 then 。若想在一行内写出完整的 if 式,
# 则必须以 then 隔开条件式和程式区块

if x==1 then puts "don't omit the then" end

# Ruby if 修饰符
# if修饰词组表示当 if 右边之条件成立时才执行 if 左边的式子。即如果 conditional 为真,则执行 code
$debug=1
print "debug\n" if $debug #很简单的条件句

# Ruby unless 语句
# unless式和 if式作用相反,即如果 conditional 为假,则执行 code。如果 conditional 为真,则执行 else 子句中指定的 code。
x=1
unless x>2 #除非x>2,否则执行下面这句,相反情况则执行else后面的
	puts "x is less then 2"
else
	puts "x is biger than 2"
end

# Ruby unless 修饰符
$var=1
print "1--this line print out\n" if $var
print "2--this line not print out \n" unless $var

$var=false
print "3--this line print out" unless $var


# Ruby case 语句
# 通常我们省略保留字 then 。若想在一行内写出完整的 when 式,则必须以 then 隔开条件式和程式区块。

case a=1 #这里还是要加case的
when a==1 then a=7 end

$age=5
case $age
	when 0..2
		puts "baby"
	when 3..6
		puts "little child"
	when 7..12
		puts "child"
	when 13..16
		puts "teenage"
	else
		puts "Other age"
end

foo=false
bar=true
quu=false

# 当case的"表达式"部分被省略时,将计算第一个when条件部分为真的表达式。
case 
when foo then puts 'foo is true'
when bar then puts 'bar is true'
when quu then puts 'quu is true'
end

# Ruby 中的循环用于执行相同的代码块若干次。

$i=0
$num=5

# 语法中 do 或 : 可以省略不写。但若要在一行内写出 while 式,则必须以 do 或 : 隔开条件式或程式区块。
while $i<$num do 
	puts " in circle line i=#{$i}"
	$i +=1
end

# Ruby while 修饰符
# 如果 while 修饰符跟在一个没有 rescue 或 ensure 子句
# 的 begin 语句后面,code 会在 conditional 判断之前执行一次。

#puts "I love you !" while true
$flag=0
begin
	puts "I love you too!"
	$flag+=1
end while $flag>5 #注意这里的flag前面加$

# Ruby until 修饰符
$i=0
$num=5
begin
	puts("在until循环语句中 i = #$i")
	$i+=1
end until $i>$num

# Ruby for 语句
for i in 0..5 do #其中do在这里可以省略
	puts "for循环局部变量的值为 #{i}"
end

# for...in 循环几乎是完全等价于:
(0..5).each do |i|
	puts "each循环局部变量的值为 #{i}"
end

#break 语句
for i in 0..5
	if i>2 then
		break
	end
	puts "break循环局部变量的值为 #{i}"
end

# Ruby next 语句,就相当于其他语言中的continue语句
for i in 0..5
	if i<2 then
		next
	end
	puts "next循环局部变量的值为 #{i}"
end

#Ruby redo 语句
# 重新开始最内部循环的该次迭代,不检查循环条件。如果在块内调用,则重新开始 yield 或 call。
for i in 0..5
	if i<2 then
		puts "局部变量的值为 #{i}"
		#redo #如果这里加redo会进入死循环
	end
end

# Ruby retry 语句
#ruby1.9以上,retry只能支持在rescue里面使用,不支持在block里面用;
n = 0 
begin 
  puts 'Trying to do something' 
  raise 'oops' 
rescue => ex 
  puts ex 
  n += 1 
  retry if n < 3 
end 
puts "Ok, I give up"


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`unless` 是 Spring Cache 抽象中的一个属性,它用于定义一个 SpEL 表达式,用于决定哪些方法调用的返回值不会被缓存。在使用 Spring Cache 时,可以在 `@Cacheable`、`@CachePut`、`@CacheEvict` 等注解中通过 `unless` 属性来配置。 下面是一个示例,使用 `unless` 属性来配置不缓存返回值为负数的方法调用: ```java @Cacheable(cacheNames = "myCache", unless = "#result < 0") public int myMethod(int arg) { // do something return result; } ``` 在上面的示例中,`unless` 属性的值是一个 SpEL 表达式,它的含义是如果方法调用的返回值小于 0,则不会将返回值缓存起来。注意,这里使用了 `#result` 变量来引用方法调用的返回值。 除了值为 SpEL 表达式的方式外,`unless` 属性还可以接受一个字符串数组,指定一个或多个 SpEL 表达式的名称。在这种情况下,需要在应用程序的配置文件中定义这些 SpEL 表达式。例如: ```java @Cacheable(cacheNames = "myCache", unless = {"#mySpELExpression"}) public int myMethod(int arg) { // do something return result; } ``` 在应用程序的配置文件中,需要定义名为 `mySpELExpression` 的 SpEL 表达式,例如: ```properties mySpELExpression= #result < 0 ``` 需要注意的是,`unless` 属性只能用于 `@Cacheable`、`@CachePut`、`@CacheEvict` 等注解中,不能用于 `@Caching` 注解中。另外,`unless` 属性指定的 SpEL 表达式不会影响方法的返回值,它只会影响缓存的行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值