# Can you detect if a method takes a block argument?

**URL:** <https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001>\
**Category:** ruby-talk\
**Created:** [8 January 2003 13:28 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001 "2003-01-08T13:28:12Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Hart](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Martin\_Hart](https://rubytalk.org/u/Martin_Hart)\
**Post date:** [8 January 2003 13:28 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/1 "2003-01-08T13:28:12Z")

</div>

Hi all, hope everybody had a great new year - it is snowing outside for  
the first time this winter 🙂

Sorry if this is common knowledge, I have done some searching of archives  
but cannot find information…  
Is there a way to tell whether or not a method takes a block argument?  
The sort of thing that I want to do is write the Client class below (from  
the pseudocode example given):

class Server  
def methodWithNoBlock(arg)  
return 1  
end

```
def methodWithBlock(arg, &blk)
	blk.call(1)
	blk.call(2)
end

```

end

# Client is the class that I will write - sort of a delegator with knobs

on  
client = Client.new(Server.new)

# If you call the method “normally” - then everything behaves as expected

client.methodWithNoBlock =\> 1

# If you call the method with a block and it doesn’t take a block, then

the method’s return value

# is passed as block argument.

client.methodWithNoBlock { |arg|  
# here arg is 1, and the method returns nil  
}

# If you call a method that takes a block, and you don’t pass one, then

the method’s return value

# is an array of the block arguments:

client.methodWithBlock =\> [1, 2]

# If you call a method that takes a block, and you do pass one, then

everything behaves as expected  
client.methodWithBlock { |arg|  
# first time arg is 1, second time it is 2  
# method returns nil  
}

The bit that I don’t know how to do is:

1. If client detects that the requested method takes a block argument, it  
should:  
a) pass the block given to it (if there was one), or  
b) create a dummy block that fills a return array, because the caller  
didn’t provide a block.

Possible? Sensible? etc.

Cheers for any help you can give,

> **···**
>
> –  
> Martin Hart Tel: +44 (0) 1582 618468  
> Arnclan Fax: +44 (0) 1582 619596  
> Union Street, E-mail: [martin@zsdfherg.com](mailto:martin@zsdfherg.com)  
> Dunstable, Beds LU6 1EX

---

<div class="post-metadata">

**Author:** ![Dalibor\_Sramek1](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@Dalibor\_Sramek1](https://rubytalk.org/u/Dalibor_Sramek1)\
**Post date:** [8 January 2003 13:47 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/2 "2003-01-08T13:47:06Z")

</div>

block\_given?

Dalibor Sramek [insula.cz](http://www.insula.cz/dali) | In the eyes of cats,  
dalibor.sramek@insula.cz | all things belong to cats.

> **···**
>
> On Wed, Jan 08, 2003 at 10:28:12PM +0900, Martin Hart wrote:
> 
> > Is there a way to tell whether or not a method takes a block argument?

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [8 January 2003 13:50 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/3 "2003-01-08T13:50:47Z")

</div>

> Is there a way to tell whether or not a method takes a block argument?

Briefly, no.

For example, Array#sort can be used with or without a block.

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Hugh\_Sasse](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@Hugh\_Sasse](https://rubytalk.org/u/Hugh_Sasse)\
**Post date:** [8 January 2003 14:05 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/4 "2003-01-08T14:05:52Z")

</div>

I think the question is, in fact, meaningless, because all methods can  
take a block, but they might not do anything with it.

gordon hgs 56 %\> irb  
irb(main):001:0\> print(3) { puts “hi there” }  
3nil  
irb(main):002:0\> print(3) {|x| puts “hello #{x}” }  
3nil  
irb(main):003:0\> print(3)  
3nil  
irb(main):004:0\>

The inverse problem, “has a block been passed?”, is answered by  
block\_given?

```
    Hugh

```

> **···**
>
> On Wed, 8 Jan 2003, Martin Hart wrote:
> 
> > Is there a way to tell whether or not a method takes a block argument?

---

<div class="post-metadata">

**Author:** ![Martin\_S\_Weber](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@Martin\_S\_Weber](https://rubytalk.org/u/Martin_S_Weber)\
**Post date:** [8 January 2003 14:30 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/5 "2003-01-08T14:30:51Z")

</div>

Quick shot, suppose ruby wizards can make this still better.

def use\_block?(invoke)  
catch (:block) do  
eval “#{invoke} do  
throw :block, true  
end”  
end or false  
end

pass the call you want to check along with arguments as string.

> use\_block?(“print(‘3\n’)”)  
> 3  
> =\> false  
> use\_block?(“[1,2,3].sort”)  
> =\> true

-Martin

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [8 January 2003 13:54 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/6 "2003-01-08T13:54:07Z")

</div>

Well, if I've well understood he want to know if the method that he call  
take a block not if the current method was called with a block.

Guy Decoux

> **···**
>
> > On Wed, Jan 08, 2003 at 10:28:12PM +0900, Martin Hart wrote:
> > 
> > > Is there a way to tell whether or not a method takes a block argument?
> 
> > block\_given?

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [8 January 2003 14:07 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/7 "2003-01-08T14:07:29Z")

</div>

> > Is there a way to tell whether or not a method takes a block argument?

> Briefly, no.

Slightly less briefy, yes.

> For example, Array#sort can be used with or without a block.

i.e. any method _can_ take a block. So the answer is always yes. I  
think Martin will have to reconsider his design.

Gavin

> **···**
>
> On Thursday, January 9, 2003, 12:50:47 AM, ts wrote:

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [8 January 2003 14:09 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/8 "2003-01-08T14:09:32Z")

</div>

And the _real_ question - does method foo _do anything with_ a block  
if one is given - seems to be unanswerable.

Gavin

> **···**
>
> On Thursday, January 9, 2003, 1:05:52 AM, Hugh wrote:
> 
> > On Wed, 8 Jan 2003, Martin Hart wrote:
> 
> > > Is there a way to tell whether or not a method takes a block argument?
> 
> > I think the question is, in fact, meaningless, because all methods can  
> > take a block, but they might not do anything with it.  
> > […]  
> > The inverse problem, “has a block been passed?”, is answered by  
> > block\_given?

---

<div class="post-metadata">

**Author:** ![Martin\_Hart](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Martin\_Hart](https://rubytalk.org/u/Martin_Hart)\
**Post date:** [8 January 2003 14:11 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/9 "2003-01-08T14:11:22Z")

</div>

On 1/8/03, 1:50:47 PM, ts [decoux@moulon.inra.fr](mailto:decoux@moulon.inra.fr) wrote regarding Re: Can  
you detect if a method takes a block argument?:

> > Is there a way to tell whether or not a method takes a block argument?

> Briefly, no.

> For example, Array#sort can be used with or without a block.

Thanks for the information Guy,

So the only way to accomplish what I want is to have all of the methods  
that I might call be able to take a block and work sensibly whether or  
not it is given, (like Array#sort). That’s not something that I want to  
do - I think it will pollute the design too much. Guess I’ll rethink 🙂

PS - When you say “Briefly, no” (as opposed to just “No”), does that mean  
that it might be possible (without hacking Ruby itself I mean) 🙂

Cheers  
Martin

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [8 January 2003 14:34 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/10 "2003-01-08T14:34:25Z")

</div>

> > use\_block?("[1,2,3].sort")
> 
> =\> true

Re-read his message

> The bit that I don't know how to do is:  
> 1. If client detects that the requested method takes a block argument, it  
> should:  
> a) pass the block given to it (if there was one), or  
> b) create a dummy block that fills a return array, because the caller  
> didn't provide a block.

You really \*always\* want to call Array#sort with a block ?

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Hugh\_Sasse](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@Hugh\_Sasse](https://rubytalk.org/u/Hugh_Sasse)\
**Post date:** [8 January 2003 14:45 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/11 "2003-01-08T14:45:16Z")

</div>

Nice. The only problem is that if the function has side effects  
data may be (adversely?) affected.

But there is a further problem with the whole question, I feel.  
If you know the method takes a block, you need to know what it  
does with the block – i.e semantics. Does it use the block once or  
many times, etc? So to be able to do anything useful with the  
knowledge that “this method takes a block”, you need more information  
than that. If you have this further information you will already  
know that it does/doesn’t use a block.

In other words, how can the information that a method uses a block  
be sufficient knowledge to use that method correctly?

```
    Hugh

```

> **···**
>
> On Wed, 8 Jan 2003, Martin Weber wrote:
> 
> > Quick shot, suppose ruby wizards can make this still better.
> > 
> > def use\_block?(invoke)  
> > catch (:block) do  
> > eval “#{invoke} do  
> > throw :block, true  
> > end”  
> > end or false  
> > end

---

<div class="post-metadata">

**Author:** ![David\_Alan\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@David\_Alan\_Black1](https://rubytalk.org/u/David_Alan_Black1)\
**Post date:** [8 January 2003 14:55 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/12 "2003-01-08T14:55:38Z")

</div>

Hi –

> Quick shot, suppose ruby wizards can make this still better.
> 
> def use\_block?(invoke)  
> catch (:block) do  
> eval “#{invoke} do  
> throw :block, true  
> end”  
> end or false  
> end
> 
> pass the call you want to check along with arguments as string.
> 
> > use\_block?(“print(‘3\n’)”)

Don’t deprive yourself of the pleasures of #puts 🙂

> 3  
> =\> false
> 
> > use\_block?(“[1,2,3].sort”)  
> > =\> true

I’m not sure what you gain here, since you only learn after the fact  
that a method had to take a block. You could call the method  
again… but it’s very possible that the first call will have changed  
the program’s state. I think you’d be better off with an underlying  
design that didn’t depend on this. Can you not just document your  
methods so that the code that invokes them knows what to do?

David

> **···**
>
> On Wed, 8 Jan 2003, Martin Weber wrote:
> 
> –  
> David Alan Black  
> home: [dblack@candle.superlink.net](mailto:dblack@candle.superlink.net)  
> work: blackdav@shu.edu  
> Web: [http://pirate.shu.edu/~blackdav](http://pirate.shu.edu/~blackdav)

---

<div class="post-metadata">

**Author:** ![Jim\_Freeze2](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@Jim\_Freeze2](https://rubytalk.org/u/Jim_Freeze2)\
**Post date:** [8 January 2003 19:28 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/13 "2003-01-08T19:28:00Z")

</div>

What about functions that take an optional block?

def optblock(&block)  
return 5 if block\_given?  
4  
end

optblock {} #=\> 5  
optblock #=\> 4  
use\_block?(“optblock”) #=\> 5

> **···**
>
> On Wednesday, 8 January 2003 at 23:30:51 +0900, Martin Weber wrote:
> 
> > Quick shot, suppose ruby wizards can make this still better.
> > 
> > def use\_block?(invoke)  
> > catch (:block) do  
> > eval “#{invoke} do  
> > throw :block, true  
> > end”  
> > end or false  
> > end
> 
> ## – Jim Freeze
> 
> If only one could get that wonderful feeling of accomplishment without  
> having to accomplish anything.

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [8 January 2003 14:12 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/14 "2003-01-08T14:12:45Z")

</div>

> i.e. any method \*can\* take a block. So the answer is always yes.

You can do that ?

> The bit that I don't know how to do is:  
> 1. If client detects that the requested method takes a block argument, it  
> should:  
> a) pass the block given to it (if there was one), or  
> b) create a dummy block that fills a return array, because the caller  
> didn't provide a block.

See b)

Try to call Array#sort with a dummy block just to see the result ...

Guy Decoux

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [8 January 2003 14:23 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/15 "2003-01-08T14:23:19Z")

</div>

> PS - When you say "Briefly, no" (as opposed to just "No"), does that mean  
> that it might be possible (without hacking Ruby itself I mean) 🙂

No, this was just because I was sure that someone else will respond  
"yes" 🙂 and his response will not solve your problem :-))

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Martin\_Hart](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Martin\_Hart](https://rubytalk.org/u/Martin_Hart)\
**Post date:** [8 January 2003 15:15 UTC](https://rubytalk.org/t/can-you-detect-if-a-method-takes-a-block-argument/4001/16 "2003-01-08T15:15:57Z")

</div>

Wow - thanks for all the responses, I’m overwhelmed with information now  
🙂

Ok, my design needs rethinking, but Martin Weber’s code snippet made me  
think a bit.

I want a program that will run in “distributed” or “local” mode without  
changing any code. While this is fairly straightforward to achieve in  
Ruby (delegation with an optional XMLRPC layer or some such), I also need  
to optimise various types of method call. For example:

A “server” has a method loadAllItems - (there could be lots of items).  
A “client” calls it.

Now in a “local” environment, it might be ok for the client to wait for  
the server to build a huge array of items and return it. In the  
“distributed” environment it probably won’t be.

So, what I thought I would do is:

server.loadItems(loadCriteria) { |item|  
# Do something with item (perhaps the block receives a chunk of items  
rather than just 1)  
}

By using delegation, this will work regardless of where the “server” is  
(same process or other side of world).

So to answer Guy’s question:

> You really _always_ want to call Array#sort with a block ?

In the case of my design (if you can call it that :-), the block doesn’t  
really have any semantics associated with the message call - it’s just an  
alternative way to receive the method’s return value/or partial return value.

[(D Black)]

> Can you not just document your methods so that the code that invokes  
> them knows what to do?

Yes -

- loadAllItems never takes a block and returns the lot
- loadItemsInChunks can always take a block pass it a chunk of items
- In distributed mode, there is a proxy layer, in local mode there isn’t.

Perhaps I am trying to be too clever? What I think I want is to function  
“sensibly” regardless of whether a block is passed. My definition of  
sensible is obviously different from everyone elses 🙂

I have been persuaded that the design is bad - I’ll rethink.

Finally, on the topic of snow,

1. Its cold and wet
2. Its too bright to look at
3. Bah, Humbug 🙂

Cheers,  
Martin
