Ruby 循环

  Ruby 中的循环用于执行相同的代码块若干次。本章节将详细介绍 Ruby 支持的所有循环语句。

  Ruby while 语句

  语法

  while conditional [do]

  code

  end

  当 conditional 为真时,执行 code。while 循环的 conditional 通过保留字 do、一个换行符、反斜线 \ 或一个分号 ; ,来与 code 分离开。

  实例

  #!/usr/bin/ruby

  $i = 0

  $num = 5

  while $i < $num do

  puts("Inside the loop i = #$i" )

  $i +=1

  end

  这将产生以下结果:

  Inside the loop i = 0

  Inside the loop i = 1

  Inside the loop i = 2

  Inside the loop i = 3

  Inside the loop i = 4

  Ruby while 修饰符

  语法

  code while condition

  或者

  begin

  code

  end while conditional

  当 conditional 为真时,执行 code。

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

  实例

  #!/usr/bin/ruby

  $i = 0

  $num = 5

  begin

  puts("Inside the loop i = #$i" )

  $i +=1

  end while $i < $num

  这将产生以下结果:

  Inside the loop i = 0

  Inside the loop i = 1

  Inside the loop i = 2

  Inside the loop i = 3

  Inside the loop i = 4

  Ruby until 语句

  until conditional [do]

  code

  end

  当 conditional 为假时,执行 code。until 语句的 conditional 通过保留字 do、一个换行符或一个分号,来与 code分离开。

  实例

  #!/usr/bin/ruby

  $i = 0

  $num = 5

  until $i > $num do

  puts("Inside the loop i = #$i" )

  $i +=1;

  end

  这将产生以下结果:

  Inside the loop i = 0

  Inside the loop i = 1

  Inside the loop i = 2

  Inside the loop i = 3

  Inside the loop i = 4

  Inside the loop i = 5

  Ruby until 修饰符

  语法

  code until conditional

  OR

  begin

  code

  end until conditional

  当 conditional 为假时,执行 code。

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

  实例

  #!/usr/bin/ruby

  $i = 0

  $num = 5

  begin

  puts("Inside the loop i = #$i" )

  $i +=1;

  end until $i > $num

  这将产生以下结果:

  Inside the loop i = 0

  Inside the loop i = 1

  Inside the loop i = 2

  Inside the loop i = 3

  Inside the loop i = 4

  Inside the loop i = 5

  Ruby for 语句

  语法

  for variable [, variable ...] in expression [do]

  code

  end

  针对 expression 中的每个元素分别执行一次 code。

  实例

  #!/usr/bin/ruby

  for i in 0..5

  puts "Value of local variable is #{i}"

  end

  在这里,我们已经定义了范围 0..5。语句 for i in 0..5 允许 i 的值从 0 到 5(包含 5)。这将产生以下结果:

  Value of local variable is 0

  Value of local variable is 1

  Value of local variable is 2

  Value of local variable is 3

  Value of local variable is 4

  Value of local variable is 5

  for...in 循环几乎是完全等价于:

  (expression).each do |variable[, variable...]| code end

  但是,for 循环不会为局部变量创建一个新的作用域。for 循环的 expression 通过保留字 do、一个换行符或一个分号,来与 code 分离开。.

  实例

  #!/usr/bin/ruby

  (0..5).each do |i|

  puts "Value of local variable is #{i}"

  end

  这将产生以下结果:

  Value of local variable is 0

  Value of local variable is 1

  Value of local variable is 2

  Value of local variable is 3

  Value of local variable is 4

  Value of local variable is 5

  Ruby break 语句

  语法

  break

  终止最内部的循环。如果在块内调用,则终止相关块的方法(方法返回 nil)。

  实例

  #!/usr/bin/ruby

  for i in 0..5

  if i > 2 then

  break

  end

  puts "Value of local variable is #{i}"

  end

  这将产生以下结果:

  Value of local variable is 0

  Value of local variable is 1

  Value of local variable is 2

  Ruby next 语句

  语法

  next

  跳到最内部循环的下一个迭代。如果在块内调用,则终止块的执行(yield 或调用返回 nil)。

  实例

  #!/usr/bin/ruby

  for i in 0..5

  if i < 2 then

  next

  end

  puts "Value of local variable is #{i}"

  end

  这将产生以下结果:

  Value of local variable is 2

  Value of local variable is 3

  Value of local variable is 4

  Value of local variable is 5

  Ruby redo 语句

  语法

  redo

  重新开始最内部循环的该次迭代,不检查循环条件。如果在块内调用,则重新开始 yield 或 call。

  实例

  #!/usr/bin/ruby

  for i in 0..5

  if i < 2 then

  puts "Value of local variable is #{i}"

  redo

  end

  end

  这将产生以下结果,并会进入一个无限循环:

  Value of local variable is 0

  Value of local variable is 0

  ............................

  Ruby retry 语句

  语法

  retry

  如果 retry 出现在 begin 表达式的 rescue 子句中,则从 begin 主体的开头重新开始。

  begin

  do_something # 抛出的异常

  rescue

  # 处理错误

  retry # 重新从 begin 开始

  end

  如果 retry 出现在迭代内、块内或者 for 表达式的主体内,则重新开始迭代调用。迭代的参数会重新评估。

  for i in 1..5

  retry if some_condition # 重新从 i == 1 开始

  end

  实例

  #!/usr/bin/ruby

  for i in 1..5

  retry if i > 2

  puts "Value of local variable is #{i}"

  end

  这将产生以下结果,并会进入一个无限循环:

  Value of local variable is 1

  Value of local variable is 2

  Value of local variable is 1

  Value of local variable is 2

  Value of local variable is 1

  Value of local variable is 2

  ............................

  本文转载自:w3cschool(编辑:雷林鹏 来源:网络)

雷林鹏分享:Ruby 循环的更多相关文章

  1. 雷林鹏分享:Ruby 命令行选项

    Ruby 命令行选项 Ruby 一般是从命令行运行,方式如下: $ ruby [ options ] [.] [ programfile ] [ arguments ... ] 解释器可以通过下列选项 ...

  2. 雷林鹏分享:Ruby 数据库访问 - DBI 教程

    Ruby 数据库访问 - DBI 教程 本章节将向您讲解如何使用 Ruby 访问数据库.Ruby DBI 模块为 Ruby 脚本提供了类似于 Perl DBI 模块的独立于数据库的接口. DBI 即 ...

  3. 雷林鹏分享:C# 循环

    C# 循环 有的时候,可能需要多次执行同一块代码.一般情况下,语句是顺序执行的:函数中的第一个语句先执行,接着是第二个语句,依此类推. 编程语言提供了允许更为复杂的执行路径的多种控制结构. 循环语句允 ...

  4. 雷林鹏分享:Ruby 安装 - Windows

    Ruby 安装 - Windows 下面列出了在 Windows 机器上安装 Ruby 的步骤. 注意:在安装时,您可能有不同的可用版本. 下载最新版的 Ruby 压缩文件.请点击这里下载. 下载 R ...

  5. 雷林鹏分享:Ruby 安装 - Unix

    Ruby 安装 - Unix 下面列出了在 Unix 机器上安装 Ruby 的步骤. 注意:在安装之前,请确保您有 root 权限. 下载最新版的 Ruby 压缩文件.请点击这里下载. 下载 Ruby ...

  6. 雷林鹏分享:Ruby 语法

    Ruby 语法 让我们编写一个简单的 Ruby 程序.所有的 Ruby 文件扩展名都是 .rb.所以,把下面的源代码放在 test.rb 文件中. #!/usr/bin/ruby -w puts &q ...

  7. 雷林鹏分享:Ruby 环境变量

    Ruby 环境变量 Ruby 解释器使用下列环境变量来控制它的行为.ENV 对象包含了所有当前设置的环境变量列表. 变量描述 DLN_LIBRARY_PATH动态加载模块搜索的路径. HOME当没有参 ...

  8. 雷林鹏分享:Ruby 类和对象

    Ruby 类和对象 Ruby 是一种完美的面向对象编程语言.面向对象编程语言的特性包括: 数据封装 数据抽象 多态性 继承 这些特性将在 面向对象的 Ruby 中进行讨论. 一个面向对象的程序,涉及到 ...

  9. 雷林鹏分享:Ruby 数据类型

    Ruby 数据类型 本章节我们将为大家介绍 Ruby 的基本数据类型. Ruby支持的数据类型包括基本的Number.String.Ranges.Symbols,以及true.false和nil这几个 ...

随机推荐

  1. UVA 475

    /* 通过这题 学会了 两个词组 immediately to the left 是左邻的意思 immediately to the right 这个是右邻的意思 */ #include <io ...

  2. 无法在web服务器下启动调试。该Web服务器未及时响应

    下午在运行项目的时候,突然出现了以下错误: 无法在web服务器上启动调试.该Web服务器未及时响应.可能是因为另一个调试器已连接到该Web服务器. 搜索了很久才找到这个解决方案: 1:Web.conf ...

  3. linux常用命令:cal 命令

    cal命令可以用来显示公历(阳历)日历.公历是现在国际通用的历法,又称格列历,通称阳历.“阳历”又名“太阳历”,系以地球绕行太阳一周为一年,为西方各国所通用,故又名“西历”. 1.命令格式: cal  ...

  4. javashop组件开发指南

    javashop组件开发指南 1.      概念解释 组件:可以理解为是插件,功能点的一个集合. 插件:是指具体的某个功能. 插件桩:是负责调用插件. 事件:是要决定什么时候执行插件 一个组件是由多 ...

  5. javascript中的console.log有什么作用?

    javascript中的console.log有什么作用? 主要是方便你调式javascript用的.你可以看到你在页面中输出的内容. 相比alert他的优点是:他能看到结构话的东西,如果是alert ...

  6. Js基础知识7-JavaScript所有内置对象属性和方法汇总

    对象什么的,程序员可是有很多呢... JS三大对象 对象,是任何一个开发者都无法绕开和逃避的话题,她似乎有些深不可测,但如此伟大和巧妙的存在,一定值得你去摸索.发现.征服. 我们都知道,JavaScr ...

  7. Mysql利用存储过程插入500W条数据

    比如插入1000W数据,不建议一次性插入,比如一次插入500W,分批插入. 创建表 /*部门表*/ CREATE TABLE dept( id INT UNSIGNED PRIMARY KEY AUT ...

  8. P1852 [国家集训队]跳跳棋

    P1852 [国家集训队]跳跳棋 lca+二分 详细解析见题解 对于每组跳棋,我们可以用一个三元组(x,y,z)表示 我们发现,这个三元组的转移具有唯一性,收束性 也就是说,把每个三元组当成点,以转移 ...

  9. 2018-2019-1 20189218《Linux内核原理与分析》第四周作业

    构造简单的Linux内核 显然用实验楼配好的环境做这个实验太简单了,按照没有困难制造困难也要上的原则,在自己的64位虚拟机上做这个实验. 按照课本(视频)上的步骤一直做下去,到编译生成init时出现了 ...

  10. JAVA I/O(五)多线程网络Socket和ServerSocket

    上篇文章介绍了Socket和ServerSocket简单使用和源码,服务器端会对每个客户端请求创建一个线程,为使服务器端能同时处理多个客户端请求,可以采用多线程的方式.本文仅对实例进行简单记录,如下. ...