7

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. 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! method?

1
  • Based on your example, you'd just use do/end or {} to create the block. If it yields something to the block, you'd use vertical bars |foo| to access whatever it yields in your block, like the each example in my comment. Commented Oct 11, 2011 at 18:03

3 Answers 3

4

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; ultimately the "right" answer depends on what you're trying to do.

Note there's an example if you're talking about Net::SSH.

Sign up to request clarification or add additional context in comments.

5 Comments

So in your example, is foo the method that I am calling? And the block gets created automatically when do/end comes next?
@Andrew foo is the method that's being called, correct. (I slightly misread your question, I thought you were also creating the method.) But yes. Things like each are methods that take a block, so aList.each { |item| puts item } is doing the same thing--the block is passed to the each method.
Thanks! You really cleared it up for me. Also, thanks for the link to the example. I'm actually using a library that wraps Net::SSH so I didn't know about that example.
oh, what happens when there is a second argument (like the optional options argument in my example)? Does it get ignored some how, or would I need to pass nil as the second argument?
@Andrew Since it has a default value specified, I don't think you'd need to pass nil--why not just try it?
3

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. 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 }

Comments

0

How about using keyword yield for understanding the passing block argument?

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 -

{ |message| puts message }.

In the our_method it executes first string and will print "we're going to call yield operator".

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. That's why the block will print string from our_method.

In the end the our_method prints final string "we called the yield operator".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.