Ruby 块的最佳解释? [英] Best explanation of Ruby blocks?

查看:27
本文介绍了Ruby 块的最佳解释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以分享的 Ruby 块的最佳解释是什么?

What is the best explanation for Ruby blocks that you can share?

用法和编写可以占用块的代码?

Both usage and writing code that can take a block?

推荐答案

我从 这个答案,稍作修改:

Ruby 中的块"与一般编程术语代码块"或代码块"不同.

"Blocks" in Ruby are not the same as the general programming terms "code block" or "block of code".

假设以下(无效的)Ruby 代码实际上有效:

Pretend for a moment that the following (invalid) Ruby code actually worked:

def add10( n )
  puts "#{n} + 10 = #{n+10}"
end

def do_something_with_digits( method )
  1.upto(9) do |i|
    method(i)
  end
end

do_something_with_digits( add10 )
#=> "1 + 10 = 11"
#=> "2 + 10 = 12"
...
#=> "9 + 10 = 19"

虽然该代码无效,但其意图(将一些代码传递给方法并让该方法运行代码)在 Ruby 中可以通过多种方式实现.其中一种方法是块".

While that code is invalid, its intent—passing some code to a method and having that method run the code—is possible in Ruby in a variety of ways. One of those ways is "Blocks".

Ruby 中的 Block 非常非常像一个方法:它可以接受一些参数并为这些参数运行代码.每当你看到 foo{ |x,y,z|... }foo 做 |x,y,z|... end,这些是带有三个参数并在其上运行 ... 的块.(您甚至可能会看到上面的 upto 方法正在传递一个块.)

A Block in Ruby is very, very much like a method: it can take some arguments and run code for those. Whenever you see foo{ |x,y,z| ... } or foo do |x,y,z| ... end, those are blocks that take three parameters and run the ... on them. (You might even see that the upto method above is being passed a block.)

因为块是 Ruby 语法的一个特殊部分,所以每个方法都可以传递一个块.方法是否使用块取决于方法.例如:

Because Blocks are a special part of the Ruby syntax, every method is allowed to be passed a block. Whether or not the method uses the block is up to the method. For example:

def say_hi( name )
  puts "Hi, #{name}!"
end

say_hi("Mom") do
  puts "YOU SUCK!"
end
#=> Hi, Mom!

上面的方法传递了一个准备发出侮辱的块,但由于该方法从不调用该块,所以只打印了好消息.下面是我们如何从方法中调用块:

The method above is passed a block that is ready to issue an insult, but since the method never calls the block, only the nice message is printed. Here's how we call the block from a method:

def say_hi( name )
  puts "Hi, #{name}!"
  if block_given?
    yield( name )
  end
end

say_hi("Mridang") do |str|
  puts "Your name has #{str.length} letters."
end
#=> Hi, Mridang!
#=> Your name has 7 letters.

我们使用 block_given? 来查看一个块是否被传递.在这种情况下,我们将一个参数传递回块;由您的方法决定将什么传递给块.例如:

We use block_given? to see whether or not a block was passed along or not. In this case we passed an argument back to the block; it's up to your method to decide what to pass to the block. For example:

def say_hi( name )
  puts "Hi, #{name}!"
  yield( name, name.reverse ) if block_given?
end

say_hi("Mridang"){ |str1, str2| puts "Is your name #{str1} or #{str2}?" }
#=> Hi, Mridang!
#=> Is your name Mridang or gnadirM?

这只是一些类将刚刚创建的实例传递给块的约定(也是一个很好的约定,也是您想要支持的约定).

It's just a convention (and a good one, and one you want to support) for some classes to pass the instance just created to the block.

这不是一个详尽的答案,因为它不包括将块捕获为参数、它们如何处理元数、块参数中的 un-splatting 等,而是打算作为 Blocks-Are-Lambdas 的介绍.

这篇关于Ruby 块的最佳解释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆