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

查看:96
本文介绍了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 do |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.

这不是一个详尽的答案,因为它没有涵盖将块作为参数捕获,它们如何处理arity,不散布块参数等,但是打算用作Blocks-Are-Lambdas简介.

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

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