At the begin of studying Ruby,I knew that you can write the following codes to print an object 3 times:
3.times {print "*"}
or print the number from 1 to 6 like this:
1.upto(6) {|i| print i}
I thought these are fixed formats and I didn't know the reason of these before this afternoon.In fact,these formats are called Blocks in Ruby."times" and "upto" are functions of the "3" and "1" objects.The following braces and sentances are defining a block and passed to these function.In these functions it use "yield" to call these blocks which were passed.Then the codes of times maybe like this:
def times
while condition
yield
end
end
and the codes of upto function maybe like this:
def upto(num)
a loop
yield(i) #there is a parameter when you call yield and pass to the block.
end loop
end
Of cause,the really codes of these functions are not so simply.
The "yield" keywork and Blocks can implement a pattern called a "Template Pattern or Strategy Pattern" in Java.You can implement the same codes in a function and pass the difference codes by Blocks.
By the way,you can use two braces to single-line Blocks or "do...end" to define multiline Blocks.
分享到:
相关推荐
在Ruby中使用块:一份脑友好的报告(Jay McGavren)是一本专注于Ruby编程语言中块(block)概念的图书。Ruby块是一种类似闭包的结构,可以在Ruby的方法调用中传递代码块,以此来处理不同的任务。本书的目标是帮助...
You'll enter at Ruby's language basics and work through progressively advanced Ruby features such as classes, inheritance, and blocks. As your Ruby skills grow, you'll tackle deep topics such as ...
This overview provides a detailed look into the key concepts and features of the Ruby programming language covered in the "Ruby Pocket Reference." It serves as a comprehensive guide for both beginners...
This book is for beginning programmers, programmers new to Ruby, and web developers interested in learning and knowing the foundations of the Ruby programming language. Table of Contents Part 1: ...
- **Numbered Parameters in Blocks**:允许在块中使用数字参数,使代码更简洁。 - **String#unpack1**:新的方法,可以从二进制字符串中提取单个元素。 - **Integer#digits**:返回一个数组,包含整数的二进制、八...
The road to Ruby mastery is paved with blocks, procs, and lambdas. To be a truly effective Ruby programmer, it’s not enough just to understand these features—you need to know how to use them in ...
* Understand the basics of Ruby and object-oriented building blocks. * Work with Ruby libraries, gems, and documentation. * Work with files and databases. * Write and deploy Ruby applications. * ...
You will find a thorough introduction to both Ruby and Rails in this book. You'll get the easy instructions for acquiring and installing both; understand the nature of conditionals, loops, methods, ...
4. **Ruby的模块系统**:Ruby中的模块(Module)可以用于封装方法和常量,实现命名空间管理,以及混合(Mix-in)功能,即把模块的方法添加到类中,增加代码重用性。 5. **Ruby on Rails框架**:虽然Ruby语言本身...
Ruby的Module提供了混入(Mix-in)特性,允许我们把模块的方法插入到类中。这常用于实现回调机制,比如ActiveSupport库中的`before_action`和`after_action`方法。这些方法允许我们定义在特定操作之前或之后执行的回...
标签“Ruby”明确了讨论的焦点是Ruby语言,这将涉及到Ruby的面向对象特性、块(blocks)、 Proc 对象以及可能的Gem库,如Monad或Functional Ruby,这些都可以用来实现单子功能。 在Ruby中实现单子通常包括以下几个...
Ruby的块(Blocks)和 Proc 对象允许你定义可重用的代码段。块可以用 `{}` 或 `do..end` 包裹,Proc 是一个可以赋值和传递的代码对象。例如: ```ruby numbers = [1, 2, 3] squares = numbers.map { |n| n**2 } ``` ...
as Java, Ruby, or Python and are interested in improving their craft by learning Scala. Java developers will recognize the core object-oriented, static typing and generic col‐ lections in Scala. ...