简体   繁体   English

Ruby:如何创建一个块并将其作为参数传递?

[英]Ruby: How to create a block and pass it as an argument?

I'm working with a method that takes a block as an argument. 我正在使用一个以块为参数的方法。 I'm new to Ruby and Blocks, so I don't quite understand how I would go about creating a Block and passing it to the method. 我是Ruby和Blocks的新手,所以我不太明白如何创建一个Block并将其传递给方法。 Can you please provide me an example of how you would create a block and pass it as an argument? 能否请您举例说明如何创建一个块并将其作为参数传递?

Update: Here is an example of the method that I am trying to call: 更新:这是我尝试调用的方法的示例:

def exec!(commands, options=nil, &block)
  # method code here
  # eventually it will execute the block if one was passed
end

Here is how I am currently calling this method: 以下是我目前调用此方法的方法:

@result = ssh.exec!("cd /some/dir; ls")

How do I pass a block as the third argument to the exec! 如何将块作为第三个参数传递给exec! method? 方法?

It depends partially on how you want to use it. 这部分取决于您希望如何使用它。 An easy way is this, if it fits your usage needs: 如果它符合您的使用需求,这是一个简单的方法:

@result = ssh.exec!("cd /some/dir; ls") do |something|
    # Whatever you need to do
    # The "something" variable only makes sense if exec! yields something
end

Or 要么

@result = ssh.exec!("cd /some/dir; ls") { |something| puts something }

The {} notation is generally used when the block is short. 当块很短时,通常使用{}表示法。

You can also create a Proc or lambda; 你也可以创建一个Proc或lambda; ultimately the "right" answer depends on what you're trying to do. 最终“正确”的答案取决于你想要做什么。

Note there's an example if you're talking about Net::SSH. 请注意,如果您正在谈论Net :: SSH,那么就有一个例子

And one more thing. 还有一件事。 You can also create Proc-object (or any object that have 'to_proc' method) and call your method with that Proc-object as last argument with '&' symbol before it. 您还可以创建Proc-object(或任何具有'to_proc'方法的对象)并使用该Proc-object作为最后一个参数调用您的方法,并在其前面加上'&'符号。 For example: 例如:

proc = Proc.new { |x| puts x }
%w{1 2 3}.each(&proc)

other way to doing the same thing: 做同样事情的其他方式:

%w{1 2 3}.each { |x| puts x }

How about using keyword yield for understanding the passing block argument? 如何使用关键字yield来理解传递块参数?

For example we have: 例如,我们有:

def our_method
  puts "we're going to call yield operator"
  yield "this is message from our_method"
  puts "we called the yield operator"
end

our_method { |message| puts message }

We will get this result: 我们将得到这个结果:

we're going to call yield operator
this is message from our_method
we called the yield operator

How it works? 这个怎么运作?

When we called the our_method we also passed to it the argument, in our case it's a block - 当我们调用our_method时,我们也将参数传递给它,在我们的例子中它是一个块 -

{ |message| puts message } { |message| puts message } . { |message| puts message }

In the our_method it executes first string and will print "we're going to call yield operator" . our_method中它执行第一个字符串并将打印“我们将调用yield运算符”

And then it's the turn of yield operator. 然后是收益率运算符的转向。 It almost equivalent to block.call but besides this it's passing the message to the block as an argument. 它几乎等同于block.call但除此之外,它将消息作为参数传递给块。 That's why the block will print string from our_method. 这就是该块将从our_method打印字符串的原因。

In the end the our_method prints final string "we called the yield operator" . 最后,our_method打印最终字符串“我们称之为yield运算符”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM