`
CherryRemind
  • 浏览: 53764 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

21 天Ruby-------- loop

    博客分类:
  • Ruby
阅读更多
class ForLoop
  
  def callFor 
    for i in 1..3
      print "index = #{i}\n"
    end
  end
  
  def callWhile
    a = 1
    a *= 2 while a < 10
    puts a
    
    a -= 2 until a < 0
    puts a
  end
  
  def callWhile2
    a = 1
    a += 1  while a < 5
    puts a
  #  print "index = #{a}\n"
  end

  def time
    3.times do
      print "Ho! "
    end
  end
  
  def upto
     1.upto(3) do |x|
      puts x
    end
  end
  
  def step
    0.step(10, 2) do |x|
      puts x
    end
  end

  def each 
    [1, 2, 3, 4, 5].each do |x|
      puts x
    end
  end
  
  def callFor2  # seems similar as each
    for n in [5, 4, 3, 2, 1]
      puts n
    end
  end
  
  def callLoop
     i = 0
     loop do
      i += 1
      puts i
      next if not i > 3
      puts "i > 3"
      redo if i >= 6 and i <= 9
      puts "i < 6 or i > 9"
      break if i == 10 
    end
  end
  
end

instance = ForLoop.new
instance.callFor
print "----------\n"
instance.callWhile2
print "----------\n"
instance.time
print "\n----------\n"
instance.upto
print "----------\n"
instance.step
print "----------\n"
instance.each
print "----------\n"
instance.callFor2
print "----------\n"
instance.callLoop

# output
#index = 1
#index = 2
#index = 3
#----------
#5
#----------
#Ho! Ho! Ho! 
#----------
#1
#2
#3
#----------
#0
#2
#4
#6
#8
#10
#----------
#1
#2
#3
#4
#5
#----------
#5
#4
#3
#2
#1
#----------
#1
#2
#3
#4
#i > 3
#i < 6 or i > 9
#5
#i > 3
#i < 6 or i > 9
#6
#i > 3
#7
#i > 3
#8
#i > 3
#9
#i > 3
#10
#i > 3
#i < 6 or i > 9



ruby loop变现形式真多
分享到:
评论

相关推荐

    Ruby-LightIO是一个ruby网络库它结合了rubyfiber和快速IOeventloop

    LightIO是一个ruby网络库,它结合了ruby fiber和快速IO event loop

    ruby-mqtt:实现MQTT协议的纯Ruby gem,MQTT协议是用于发布订阅消息传递的轻量级协议

    Ruby实现协议(一种用于发布/订阅消息传递的轻量级协议)的纯Ruby gem。... connect ( 'test.mosquitto.org' ) do | c | # If you pass a block to the get method, then it will loop c . get ( 'test'

    sketchup-loop-subdivision:Sketchup插件可使用循环细分来平滑模型

    #sketchup-loop-subdivision适用于Google Sketchup的Loop细分插件。 循环细分可平滑硬边缘,以提供更圆润的有机外观。 ## Installing从下载文件loop_subdiv.rb并将其保存在Google Sketchup插件目录中。 在Windows...

    ruby-event-framework:事件框架是一个提供发布订阅模式的简约库

    Ruby 事件框架 事件框架是一个提供发布订阅模式的简约库。 安装 gem install event-framework 例子 require 'event-framework' class Server include EF :: Object end class Client include EF :: Object end ...

    looping-loop-v-000

    这是我们在Ruby中最简单的循环构造。 它只是执行一个块(位于do和end关键字之间的代码)。 在您的终端的IRB中尝试以下操作: loop do puts "I have found the Time Machine!" end 这将输出I have found the Time...

    ruby语法基础教程

    §3.4 特殊方法与特殊类 21 §3.5 类变量与类方法 23 §3.4 存取控制 23 §3.6 元类 25 §3.7 Ruby的动态性 26 §3.8 变量 26 §3.8.1 局部变量 27 §3.8.2 实例变量 27 §3.8.3 类变量 27 §3.8.4 全局变量 28 §...

    ttt-9-play-loop-online-web-sp-000

    井字游戏循环 目标 在方法中使用循环。 在CLI中使用循环方法。 在循环中调用方法。 概述 在本实验中,您将为Tic Tac Toe构建一个#play方法,该方法将启动一个简单的循环并调用#turn 。它的功能不会像井字游戏的真实...

    human-in-the-loop-machine-learning-tool-tornado:Tornado是一个人在环境的机器学习框架,可帮助您通过简单易用的Web界面利用未标记的数据来训练模型。

    龙卷风人类在循环机器学习中什么是龙卷风Tornado是一种开源的“人在环”机器学习工具。 它可以帮助您在通过简单的Web用户界面训练模型时动态标记数据集。现场演示数据流技术栈构建Tornado Docker映像$ docker-...

    eventkit-eventloop:非阻塞 IO 的基本事件循环 :motorway:

    使用 Ruby 的 IO.select 实现的基本事件循环以执行非阻塞 IO。 安装 将此行添加到应用程序的 Gemfile 中: gem 'eventkit-eventloop' 事件循环使用 Eventkit 在 Ruby 的 IO.select 之上提供了一个基本的事件循环...

    C事件驱动循环的网络IO编程框架的源码.rar

    基于事件驱动循环的网络IO编程框架,小编英语学的不好,直接把英原文复制出来,如下: ... - support for other languages (multiple C interfaces, D, Ruby, Python) available from third-parties.

    http-2:HTTP2协议的纯Ruby实现

    请参阅协议规格: 入门$ &gt; gem install http-2 此实现不假设数据如何传递:它可以是常规的Ruby TCP套接字,您的自定义eventloop或您希望使用的任何其他传输方式(例如ZeroMQ,等)。 您的代码负责将数据馈送到解析器...

    Continuous Testing: With Ruby, Rails, and JavaScript

    It also helps you improve the quality of the new tests you write, by giving you instant feedback about problem areas, and creating a visceral feedback loop for test quality that you can ...

    ruby 局部变量

    局部变量由小写字母或下划线(_)开头.局部变量不像全局和实变量一样在初始化前含nil值. ruby&gt; $foo nil ruby&gt; @foo nil ruby&gt; foo ERR: (eval):1: ...proc{…} loop{…} def…end class…end module…end

    swee:ruby 实现的轻量级web框架

    内部一些特性都是基于 event loop 如: 服务器重启, 以及代码reload等特性 处理请求并包装为 request 结构暂时使用的是 Thin 的 http_parser 应用层轻量级包装 实现 route 和 controller 目前支持 rails 的一些特性 ...

    eventkit:捆绑 eventkit-promise 和 eventkit-eventloop

    事件包 异步事件驱动应用程序的基本工具包。 该工具包包括一个用于执行非阻塞 IO 的事件循环和一个用于协调异步任务的 promises A+ 实现。 这个 gem 捆绑了和 gem。 安装 将此行添加到应用程序的 Gemfile 中: ...

    model-class-methods-reading-online-web-sp-000

    模型类方法 目标 知道何时使用模型类方法 创建用于自定义查询的模型类方法 课 我们将继续致力于博客应用程序并添加更多功能,因此请确保按照自己...-- add this new code above the @posts.each loop --&gt; &lt; h3&gt; F

    Ruby中的循环语句的用法教程

    Ruby中的循环用于执行相同的代码块指定的次数。本章将详细介绍Ruby支持的循环语句。 Ruby while 语句: 语法: while conditional [do]  code end 执行代码当条件为true时。while循环的条件是代码中的保留字,换行,...

    Ruby最简单的消息服务器代码

    ser.rb 代码如下:require ‘socket’s = TCPServer.new 3333conn = s.acceptloop do puts ... 您可能感兴趣的文章:Ruby实现的各种排序算法Ruby实现的合并排序算法Ruby实现的3种快速排序算法Ruby一行代码实现的快速

    multi-language-bench:多语言基准(C ++ JavaScalaGoC#F#Ruby)

    此存储库是从位于的Google代码存储库中导入的 这是该项目的原始描述: ...An initial comparison of a loop recognition algorithm, implemented in C++, Java, Go, and Scala, has been published

Global site tag (gtag.js) - Google Analytics