ruby
程序员文章站
2022-07-14 12:33:20
...
ruby的if else如何写:
另一种写法
流程控制:
break = Teminate the whole loop
next = Jump to the next loop
redo = Redo this loop
retry = Start the whole loop over
break例子:
next例子
while例子
while boolean
....
end
until boolean
...
end
Iterator:
if x < 10 puts "Below 10" elsif x > 20 puts "Over 20" else puts "10-20" end
另一种写法
puts "this is kevin" if name == "kevin" boolean ? code1 : code2 case test_value when value ... when value ... else .... end
流程控制:
break = Teminate the whole loop
next = Jump to the next loop
redo = Redo this loop
retry = Start the whole loop over
break例子:
x = 0 loop do x += 2 break if x >= 20 puts x end
next例子
loop do x += 2 break if x >= 20 next if x == 6 puts x end
while例子
while boolean
....
end
until boolean
...
end
Iterator:
5.times do puts "Hello" end 5.times {puts "Hello"} 1.upto(5) {puts "Hello"} 5.downto(1) {puts "hello"} (1..5).each{puts "Hello"} 1.upto(5) do |i| puts "Hello" + i.to_s end fruits = ['banana', 'apple', 'pear'] fruits.each do |fruit| puts fruit.capitalize end for fruit in fruits puts fruit.capitalize end array = [1,2,3] array.each {|num| puts num * 20}
上一篇: shell 文件操作
下一篇: Linux定时任务系统Cron入门
推荐阅读
-
详细介绍Ruby中的正则表达式
-
Ruby使用eventmachine为HTTP服务器添加文件下载功能
-
C#实现Ruby的负数索引器
-
Annyong 基于ruby的将本地目录变身 Web 服务器
-
HPB芯链汪晓明将出席Ruby峰会 与*同台分享Ruby开发经验
-
redis集群错误解决:/usr/lib/ruby/gems/1.8/gems/redis-3.0.0/lib/redis/client.rb:79:in `call': ERR Slot 15495 is already busy (Redis::CommandError)
-
使用Ruby脚本部署Redis Cluster集群步骤讲解
-
Ruby中遍历目录的简洁方法
-
ruby实现的文件自删除代码分享
-
Ruby、PHP、Shell实现求50以内的素数