# Send() with a block?

**URL:** <https://rubytalk.org/t/send-with-a-block/62112>\
**Category:** ruby-talk\
**Created:** [12 March 2011 20:45 UTC](https://rubytalk.org/t/send-with-a-block/62112 "2011-03-12T20:45:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [12 March 2011 20:45 UTC](https://rubytalk.org/t/send-with-a-block/62112/1 "2011-03-12T20:45:39Z")

</div>

Why don't the ruby docs say that send() can take a block?

Kernel.send(:define\_method, :my\_meth) do |x|  
&nbsp;&nbsp;puts x  
end

my\_meth(10)

--output:--  
10

At the following link there is nothing saying that send() will accept a  
block:

[http://www.ruby-doc.org/core/classes/Object.html#M000999](http://www.ruby-doc.org/core/classes/Object.html#M000999)

Whereas, for example, at the following link it states that grep() will  
take a block:

[http://www.ruby-doc.org/core/classes/Enumerable.html#M001482](http://www.ruby-doc.org/core/classes/Enumerable.html#M001482)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [12 March 2011 21:01 UTC](https://rubytalk.org/t/send-with-a-block/62112/2 "2011-03-12T21:01:31Z")

</div>

It's not the send() that's consuming the block - it's the define\_method().

Regards,  
Sean

> **···**
>
> On Sat, Mar 12, 2011 at 8:45 PM, 7stud -- \<bbxx789\_05ss@yahoo.com\> wrote:
> 
> > Why don't the ruby docs say that send() can take a block?
> > 
> > Kernel.send(:define\_method, :my\_meth) do |x|  
> > puts x  
> > end

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [12 March 2011 22:35 UTC](https://rubytalk.org/t/send-with-a-block/62112/3 "2011-03-12T22:35:33Z")

</div>

From the pickaxe:

obj.send( symbol ⟨ , args ⟩∗ ⟨ , &block ⟩ ) → other\_obj

> **···**
>
> On Mar 12, 2011, at 12:45 , 7stud -- wrote:
> 
> > Why don't the ruby docs say that send() can take a block?

---

<div class="post-metadata">

**Author:** ![Marc\_Heiler](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Marc\_Heiler](https://rubytalk.org/u/Marc_Heiler)\
**Post date:** [13 March 2011 04:29 UTC](https://rubytalk.org/t/send-with-a-block/62112/4 "2011-03-13T04:29:49Z")

</div>

> ALL methods take a block, they just don't have to do anything with it

One of the best replies. And beautifully signifies the flexible approach  
taken by Ruby.

Blocks rule.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)\
**Post date:** [14 March 2011 02:30 UTC](https://rubytalk.org/t/send-with-a-block/62112/5 "2011-03-14T02:30:57Z")

</div>

It says "Invokes the method identified by symbol, passing it any arguments  
specified." A block is an argument, it just gets set into an implicit  
location by default, or you can make it explicit, putting it into a Proc  
object, by putting &varname on the end of your params list.

I've had difficulty following the code examples in this thread. Here is the  
one I think is relevant:

def meth(first, second=12 , \*rest, &block)  
&nbsp;&nbsp;[first , second , rest , block]  
end

send :meth , 1 # =\> [1, 12, , nil]  
send :meth , 1 , 2 # =\> [1, 2, , nil]  
send :meth , 1 , 2 , 3 # =\> [1, 2, [3], nil]  
send :meth , 1 , 2 , 3 do end # =\> [1, 2, [3], #\<Proc:0x00000100868340@-:8\>]

You can see, it passes on ordinal arguments, args with default values,  
variable lengthed arguments, and yes, also blocks.

> **···**
>
> On Sat, Mar 12, 2011 at 2:45 PM, 7stud -- \<bbxx789\_05ss@yahoo.com\> wrote:
> 
> > Why don't the ruby docs say that send() can take a block?
> > 
> > Kernel.send(:define\_method, :my\_meth) do |x|  
> > puts x  
> > end
> > 
> > my\_meth(10)
> > 
> > --output:--  
> > 10
> > 
> > At the following link there is nothing saying that send() will accept a  
> > block:
> > 
> > [class Object - RDoc Documentation](http://www.ruby-doc.org/core/classes/Object.html#M000999)
> > 
> > Whereas, for example, at the following link it states that grep() will  
> > take a block:
> > 
> > [module Enumerable - RDoc Documentation](http://www.ruby-doc.org/core/classes/Enumerable.html#M001482)
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [14 March 2011 22:24 UTC](https://rubytalk.org/t/send-with-a-block/62112/6 "2011-03-14T22:24:00Z")

</div>

Thanks for all the responses. This may be true:

> **···**
>
> --  
> The actual method "send" can take a block, and it passes it to the  
> method being invoked. If it didn't, it would be impossible to pass a  
> block to a method when invoking it using send.  
> --
> 
> ...but why should a beginner have to figure that out? You shouldn't  
> have to be a meta-programming guru to figure out the docs. Wouldn't it  
> be better if beginners posted: "I really think the docs are great."  
> rather than "beginner: the docs are confusing; expert-response: they are  
> clear if you are smart."
> 
> Why doesn't this work:
> 
> def my\_meth(\*args)  
> &nbsp;&nbsp;yield  
> &nbsp;&nbsp;p args  
> end
> 
> my\_proc = Proc.new {puts 'hello'}  
> my\_meth(1, 2, 3, &my\_proc ) #=\>'hello' [1, 2, 3]
> 
> obj = Object.new  
> m = obj.method(my\_meth)  
> m.call([10, 20, 30], &my\_proc) #LocalJumpError at yield line
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Peter\_Zotov](https://avatars.discourse-cdn.com/v4/letter/p/bbce88/32.png) [@Peter\_Zotov](https://rubytalk.org/u/Peter_Zotov)\
**Post date:** [12 March 2011 21:11 UTC](https://rubytalk.org/t/send-with-a-block/62112/7 "2011-03-12T21:11:13Z")

</div>

But send() still bothers to pass the block around. Consider the following code:

---8\<---8\<---  
def my\_method  
&nbsp;&nbsp;&nbsp;p block\_given?  
end

def my\_send # imitating send  
&nbsp;&nbsp;&nbsp;my\_method  
end

my\_send # =\> false  
---8\<---8\<---

> **···**
>
> On Sun, 13 Mar 2011 06:01:31 +0900, Sean O'Halpin wrote:
> 
> > On Sat, Mar 12, 2011 at 8:45 PM, 7stud -- \<bbxx789\_05ss@yahoo.com\> \> wrote:
> > 
> > > Why don't the ruby docs say that send() can take a block?
> > > 
> > > Kernel.send(:define\_method, :my\_meth) do |x|  
> > > puts x  
> > > end
> > 
> > It's not the send() that's consuming the block - it's the define\_method().
> 
> --  
> &nbsp;&nbsp;&nbsp;WBR, Peter Zotov.

---

<div class="post-metadata">

**Author:** ![Aaron\_D\_Gifford](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@Aaron\_D\_Gifford](https://rubytalk.org/u/Aaron_D_Gifford)\
**Post date:** [14 March 2011 03:47 UTC](https://rubytalk.org/t/send-with-a-block/62112/8 "2011-03-14T03:47:29Z")

</div>

Correct, a block IS an argument, but of a rather unusual type in that  
it may or may not be explicit in the definition of a method that uses  
it.

HOWEVER, if a method (call it method bar or baz) passes any block it  
receives on to yet another method (call it method foo), it is best  
that the documents include this fact.

AN EXAMPLE:  
## Method foo() may or may not be given a block...  
def foo(\*args)  
&nbsp;&nbsp;puts "foo(#{args.inspect}) called with" + (block\_given? ? '' :  
'out') + " a block included."  
&nbsp;&nbsp;yield \*args if block\_given?  
end

## Method bar() does NOT explicitly capture block arguments  
## and thus does NOT pass any given block on to foo():  
def bar(\*args)  
&nbsp;&nbsp;puts "bar(#{args.inspect}) called with" + (block\_given? ? '' :  
'out') + " a block included."  
&nbsp;&nbsp;foo(\*args)  
end

## Method baz() DOES explicitly capture block arguments  
## and DOES pass any given block on to foo():  
def baz(\*args, &block)  
&nbsp;&nbsp;puts "baz(#{args.inspect}) called with" + (block\_given? ? '' :  
'out') + " a block included."  
&nbsp;&nbsp;foo(\*args, &block)  
end

bar("some", :args =\> "for bar", :without =\> "a block")  
puts  
bar("some", :args =\> "for bar", :with =\> "a block") { |\*a| puts "block  
passed to bar(): #{a.inspect}" }  
puts "--"  
baz("some", :args =\> "for baz", :without =\> "a block")  
puts  
baz("some", :args =\> "for baz", :with =\> "a block") { |\*a| puts "block  
passed to baz(): #{a.inspect}" }

SAMPLE OUTPUT FROM ABOVE EXAMPLE:  
bar(["some", {:args=\>"for bar", :without=\>"a block"}]) called without  
a block included.  
foo(["some", {:args=\>"for bar", :without=\>"a block"}]) called without  
a block included.

bar(["some", {:args=\>"for bar", :with=\>"a block"}]) called with a  
block included.  
foo(["some", {:args=\>"for bar", :with=\>"a block"}]) called without a  
block included.

> **···**
>
> On Sun, Mar 13, 2011 at 8:30 PM, Josh Cheek \<josh.cheek@gmail.com\> wrote:
> 
> > It says "Invokes the method identified by symbol, passing it any arguments  
> > specified." A block is an argument...
> 
> --  
> baz(["some", {:args=\>"for baz", :without=\>"a block"}]) called without  
> a block included.  
> foo(["some", {:args=\>"for baz", :without=\>"a block"}]) called without  
> a block included.
> 
> baz(["some", {:args=\>"for baz", :with=\>"a block"}]) called with a  
> block included.  
> foo(["some", {:args=\>"for baz", :with=\>"a block"}]) called with a  
> block included.  
> block passed to baz(): ["some", {:args=\>"for baz", :with=\>"a block"}]
> 
> THINGS TO NOTE FROM THE EXAMPLE:  
> \* Notice that even though bar() uses the splat to capture and pass  
> through all supplied arguments, because it does NOT explicitly include  
> a &block argument, it does NOT automatically pass through any provided  
> block.=  
> \* Note that baz() DOES explicitly include a &block argument and  
> explicitly passes it through.
> 
> When I read documents and see a method that does NOT explicitly  
> mention a block, I assume that it will NOT pass any supplied block on  
> to yet another method.
> 
> Hence, I expect that the send() documentation really should mention  
> that any block passed to it IS passed through to the method specified  
> as the first argument to send().
> 
> Maybe I'm eccentric, but I prefer that documentation for a method  
> which uses a block, either directly or by passing it through to  
> another method, whether the block is explicit or implicit in the  
> method definition, to clearly document what the block is used for or  
> what is done with it.
> 
> Aaron out.

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [14 March 2011 22:42 UTC](https://rubytalk.org/t/send-with-a-block/62112/9 "2011-03-14T22:42:31Z")

</div>

> Why doesn't this work:
> 
> def my\_meth(\*args)  
> yield  
> p args  
> end
> 
> my\_proc = Proc.new {puts 'hello'}  
> my\_meth(1, 2, 3, &my\_proc ) #=\>'hello' [1, 2, 3]
> 
> obj = Object.new  
> m = obj.method(my\_meth)  
> m.call([10, 20, 30], &my\_proc) #LocalJumpError at yield line

Your comment about LocalJumpError should be one line above and it  
occurs because you are calling my\_meth without a block and are  
attempting to yield to the non-existent block. You don't want  
to invoke my\_meth there, you want to \*name\* it:

> m = obj.method('my\_meth')

I think you are adding a confusing difference when you try to invoke  
my\_method on the last line by packaging up three arguments in an array.  
If you want to follow the pattern you used when you called my\_meth  
directly you should be doing:

> m.call(10, 20, 30, &my\_proc)

Gary Wright

> **···**
>
> On Mar 14, 2011, at 6:24 PM, 7stud -- wrote:

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [12 March 2011 21:50 UTC](https://rubytalk.org/t/send-with-a-block/62112/10 "2011-03-12T21:50:22Z")

</div>

I'm not sure what you're showing me here. But consider this:

class Foo  
&nbsp;&nbsp;def self.send(\*a, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;p block\_given?  
&nbsp;&nbsp;&nbsp;&nbsp;super(\*a)  
&nbsp;&nbsp;end  
end

class Bar  
&nbsp;&nbsp;def self.send(\*a)  
&nbsp;&nbsp;&nbsp;&nbsp;p block\_given?  
&nbsp;&nbsp;&nbsp;&nbsp;super(\*a)  
&nbsp;&nbsp;end  
end

Foo.send(:define\_method, :my\_method) do |x|  
&nbsp;&nbsp;p x  
end

block = proc{ |x| p x }  
Bar.send(:define\_method, :my\_method2, &block)

foo = Foo.new  
foo.my\_method(1)  
bar = Bar.new  
bar.my\_method2(2)

def my\_method(x)  
&nbsp;&nbsp;p x  
end

my\_method(10, &block)

\_\_END\_\_  
true  
true  
1  
2  
10

Even when you explicitly do /not/ pass on the block (as in the  
super(\*a) call above), send() does not consume it.  
Passing a block using the &block syntax is simply part of ruby's  
method calling syntax.  
A method does not have to specify that it takes a block for you to  
call it with one.

Regards,  
Sean

> **···**
>
> On Sat, Mar 12, 2011 at 9:11 PM, Peter Zotov \<whitequark@whitequark.org\> wrote:
> 
> > On Sun, 13 Mar 2011 06:01:31 +0900, Sean O'Halpin wrote:
> > 
> > > On Sat, Mar 12, 2011 at 8:45 PM, 7stud -- \<bbxx789\_05ss@yahoo.com\> wrote:
> > > 
> > > > Why don't the ruby docs say that send() can take a block?
> > > > 
> > > > Kernel.send(:define\_method, :my\_meth) do |x|  
> > > > puts x  
> > > > end
> > > 
> > > It's not the send() that's consuming the block - it's the define\_method().
> > 
> > But send() still bothers to pass the block around. Consider the following  
> > code:
> > 
> > ---8\<---8\<---  
> > def my\_method  
> > p block\_given?  
> > end
> > 
> > def my\_send # imitating send  
> > my\_method  
> > end
> > 
> > my\_send # =\> false  
> > ---8\<---8\<---
> > 
> > --  
> > WBR, Peter Zotov.

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [13 March 2011 14:26 UTC](https://rubytalk.org/t/send-with-a-block/62112/11 "2011-03-13T14:26:01Z")

</div>

Peter Zotov wrote in post #987115:

> > define\_method().
> 
> But send() still bothers to pass the block around. Consider the  
> following code:
> 
> ---8\<---8\<---  
> def my\_method  
> &nbsp;&nbsp;&nbsp;p block\_given?  
> end
> 
> def my\_send # imitating send  
> &nbsp;&nbsp;&nbsp;my\_method  
> end
> 
> my\_send # =\> false  
> ---8\<---8\<---

But compare with this:

def my\_method  
&nbsp;&nbsp;p block\_given?  
end

def my\_send(&blk) # imitating send  
&nbsp;&nbsp;my\_method(&blk)  
end

my\_send # =\> false  
my\_send { :wibble } # =\> true

> **···**
>
> > On Sun, 13 Mar 2011 06:01:31 +0900, Sean O'Halpin wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)\
**Post date:** [14 March 2011 04:36 UTC](https://rubytalk.org/t/send-with-a-block/62112/12 "2011-03-14T04:36:56Z")

</div>

> Correct, a block IS an argument, but of a rather unusual type in that  
> it may or may not be explicit in the definition of a method that uses  
> it.

There is always a block slot, as Ryan said "ALL methods take a block". What  
is explicit is whether or not you wish to assign it to a named object. Even  
then, you can turn the implicit block into a Proc object, if you need.

def meth  
&nbsp;&nbsp;proc.call  
end

meth { 1 + 1 } # =\> 2

When I read documents and see a method that does NOT explicitly

> mention a block, I assume that it will NOT pass any supplied block on  
> to yet another method.
> 
> Hence, I expect that the send() documentation really should mention  
> that any block passed to it IS passed through to the method specified  
> as the first argument to send().

This is really all you needed to say. I think all your code detracted from  
this point.

> **···**
>
> On Sun, Mar 13, 2011 at 10:47 PM, Aaron D. Gifford \<astounding@gmail.com\>wrote:

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [14 March 2011 22:56 UTC](https://rubytalk.org/t/send-with-a-block/62112/13 "2011-03-14T22:56:21Z")

</div>

Gary Wright wrote in post #987429:

> **···**
>
> > On Mar 14, 2011, at 6:24 PM, 7stud -- wrote:
> > 
> > > obj = Object.new  
> > > m = obj.method(my\_meth)  
> > > m.call([10, 20, 30], &my\_proc) #LocalJumpError at yield line
> > 
> > Your comment about LocalJumpError should be one line above and it  
> > occurs because you are calling my\_meth without a block and are  
> > attempting to yield to the non-existent block. You don't want  
> > to invoke my\_meth there, you want to \*name\* it:
> > 
> > > m = obj.method('my\_meth')
> 
> Ah, a typo. Thanks.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [12 March 2011 22:35 UTC](https://rubytalk.org/t/send-with-a-block/62112/14 "2011-03-12T22:35:33Z")

</div>

Indeed... Take that statement a step further: ALL methods take a block, they just don't have to do anything with it:

% ruby -e 'p 1.+(2) { p 42 }'  
3

> **···**
>
> On Mar 12, 2011, at 13:50 , Sean O'Halpin wrote:
> 
> > A method does not have to specify that it takes a block for you to  
> > call it with one.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [13 March 2011 13:20 UTC](https://rubytalk.org/t/send-with-a-block/62112/15 "2011-03-13T13:20:35Z")

</div>

> > > > Why don't the ruby docs say that send() can take a block?
> > > > 
> > > > Kernel.send(:define\_method, :my\_meth) do |x|  
> > > > &nbsp;&nbsp;puts x  
> > > > end
> > > 
> > > It's not the send() that's consuming the block - it's the define\_method().
> > 
> > But send() still bothers to pass the block around. Consider the following  
> > code:
> > 
> > ---8\<---8\<---  
> > def my\_method  
> > &nbsp;&nbsp;p block\_given?  
> > end
> > 
> > def my\_send # imitating send  
> > &nbsp;&nbsp;my\_method  
> > end
> > 
> > my\_send # =\> false  
> > ---8\<---8\<---
> > 
> > --  
> > &nbsp;&nbsp;WBR, Peter Zotov.
> 
> I'm not sure what you're showing me here. But consider this:
> 
> class Foo  
> &nbsp;&nbsp;&nbsp;def self.send(\*a,&block)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p block\_given?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(\*a)  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> class Bar  
> &nbsp;&nbsp;&nbsp;def self.send(\*a)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p block\_given?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(\*a)  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> Foo.send(:define\_method, :my\_method) do |x|  
> &nbsp;&nbsp;&nbsp;p x  
> end
> 
> block = proc{ |x| p x }  
> Bar.send(:define\_method, :my\_method2,&block)
> 
> foo = Foo.new  
> foo.my\_method(1)  
> bar = Bar.new  
> bar.my\_method2(2)
> 
> def my\_method(x)  
> &nbsp;&nbsp;&nbsp;p x  
> end
> 
> my\_method(10,&block)
> 
> \_\_END\_\_  
> true  
> 1  
> 2  
> 10
> 
> Even when you explicitly do /not/ pass on the block (as in the  
> super(\*a) call above), send() does not consume it.

Automatic propagation of the block is not a feature of #send but a feature of "super":

\>\> class X  
\>\> def a;p block\_given? end  
\>\> def b;p block\_given?;a end  
\>\> def c;p block\_given?;send(:a) end  
\>\> end  
=\> nil  
\>\> X.new.a {1+2}  
true  
=\> true  
\>\> X.new.b {1+2}  
true  
false  
=\> false  
\>\> X.new.c {1+2}  
true  
false  
=\> false

\>\> class A  
\>\> def a;p block\_given? end  
\>\> end  
=\> nil  
\>\> class B \< A  
\>\> def a;p block\_given?;super end  
\>\> end  
=\> nil  
\>\> class C \< A  
\>\> def a;p block\_given?;super() end  
\>\> end  
=\> nil  
\>\> A.new.a {1+2}  
true  
=\> true  
\>\> B.new.a {1+2}  
true  
=\> true  
\>\> C.new.a {1+2}  
true  
=\> true

> Passing a block using the&block syntax is simply part of ruby's  
> method calling syntax.  
> A method does not have to specify that it takes a block for you to  
> call it with one.

Correct.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 12.03.2011 22:50, Sean O'Halpin wrote:
> 
> > On Sat, Mar 12, 2011 at 9:11 PM, Peter Zotov\<whitequark@whitequark.org\> wrote:
> > 
> > > On Sun, 13 Mar 2011 06:01:31 +0900, Sean O'Halpin wrote:
> > > 
> > > > On Sat, Mar 12, 2011 at 8:45 PM, 7stud --\<bbxx789\_05ss@yahoo.com\> wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Peter\_Zotov](https://avatars.discourse-cdn.com/v4/letter/p/bbce88/32.png) [@Peter\_Zotov](https://rubytalk.org/u/Peter_Zotov)\
**Post date:** [13 March 2011 14:54 UTC](https://rubytalk.org/t/send-with-a-block/62112/16 "2011-03-13T14:54:24Z")

</div>

That's the whole point of my demonstration. (Maybe I was too concise.)  
Any send-like method should to a trick like that to get the block-passing  
behavior. It is not implicit.

> **···**
>
> On Sun, 13 Mar 2011 23:26:01 +0900, Brian Candler wrote:
> 
> > Peter Zotov wrote in post #987115:
> > 
> > > On Sun, 13 Mar 2011 06:01:31 +0900, Sean O'Halpin wrote:
> > > 
> > > > define\_method().
> > > 
> > > But send() still bothers to pass the block around. Consider the  
> > > following code:
> > > 
> > > ---8\<---8\<---  
> > > def my\_method  
> > > &nbsp;&nbsp;&nbsp;p block\_given?  
> > > end
> > > 
> > > def my\_send # imitating send  
> > > &nbsp;&nbsp;&nbsp;my\_method  
> > > end
> > > 
> > > my\_send # =\> false  
> > > ---8\<---8\<---
> > 
> > But compare with this:
> > 
> > def my\_method  
> > &nbsp;&nbsp;p block\_given?  
> > end
> > 
> > def my\_send(&blk) # imitating send  
> > &nbsp;&nbsp;my\_method(&blk)  
> > end
> > 
> > my\_send # =\> false  
> > my\_send { :wibble } # =\> true
> 
> --  
> &nbsp;&nbsp;&nbsp;WBR, Peter Zotov.

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [13 March 2011 16:18 UTC](https://rubytalk.org/t/send-with-a-block/62112/17 "2011-03-13T16:18:16Z")

</div>

> > Peter Zotov wrote in post #987115:
> > 
> > > > define\_method().
> > > 
> > > But send() still bothers to pass the block around. Consider the  
> > > following code:
> > > 
> > > ---8\<---8\<---  
> > > def my\_method  
> > > p block\_given?  
> > > end
> > > 
> > > def my\_send # imitating send  
> > > my\_method  
> > > end
> > > 
> > > my\_send # =\> false  
> > > ---8\<---8\<---
> > 
> > But compare with this:
> > 
> > def my\_method  
> > p block\_given?  
> > end
> > 
> > def my\_send(&blk) # imitating send  
> > my\_method(&blk)  
> > end
> > 
> > my\_send # =\> false  
> > my\_send { :wibble } # =\> true
> 
> That's the whole point of my demonstration. (Maybe I was too concise.)

You were so concise you left out the thing you were trying to demonstrate:

def my\_send  
&nbsp;&nbsp;p block\_given?  
end

my\_send { p 42 } #=\> true

> Any send-like method should to a trick like that to get the block-passing  
> behavior. It is not implicit.

It /is/ implicit and is true for any method call. That's how you can do this:

def my\_yield  
&nbsp;&nbsp;yield  
end

my\_yield { p 42 } #=\> 42

You specify the &block parameter when you want to do something with the block.

Regards,  
Sean

> **···**
>
> On Sun, Mar 13, 2011 at 2:54 PM, Peter Zotov \<whitequark@whitequark.org\> wrote:
> 
> > On Sun, 13 Mar 2011 23:26:01 +0900, Brian Candler wrote:
> > 
> > > > On Sun, 13 Mar 2011 06:01:31 +0900, Sean O'Halpin wrote:

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [13 March 2011 16:23 UTC](https://rubytalk.org/t/send-with-a-block/62112/18 "2011-03-13T16:23:50Z")

</div>

Peter Zotov wrote in post #987191:

> That's the whole point of my demonstration. (Maybe I was too concise.)  
> Any send-like method should to a trick like that to get the  
> block-passing  
> behavior. It is not implicit.

Indeed. And the O.P. was passing a block to send explicitly too.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [13 March 2011 17:00 UTC](https://rubytalk.org/t/send-with-a-block/62112/19 "2011-03-13T17:00:30Z")

</div>

Or to put it another way:

def my\_method  
&nbsp;&nbsp;p block\_given?  
end

def my\_send # imitating send  
&nbsp;&nbsp;my\_method  
end

my\_send # =\> false  
my\_send { :wibble } # =\> false

def my\_send\_2  
&nbsp;&nbsp;send :my\_method  
end

my\_send\_2 # =\> false  
my\_send\_2 { :wibble } # =\> false

It's entirely consistent: false in all four cases.

The actual method "send" can take a block, and it passes it to the  
method being invoked. If it didn't, it would be impossible to pass a  
block to a method when invoking it using send.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Aaron\_D\_Gifford](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@Aaron\_D\_Gifford](https://rubytalk.org/u/Aaron_D_Gifford)\
**Post date:** [13 March 2011 23:40 UTC](https://rubytalk.org/t/send-with-a-block/62112/20 "2011-03-13T23:40:38Z")

</div>

I agree that the documentation the original post linked to really  
should mention that if any block is passed to send, the block is  
passed on to the method invoked.

From the linked docs: "Invokes the method identified by symbol,  
passing it any arguments specified. You can use send if the name send  
clashes with an existing method in obj."

That could use an additional sentence: "If a block is supplied to  
send, the block is supplied to the method invoked."

Aaron out.

> **···**
>
> On Sun, Mar 13, 2011 at 11:00 AM, Brian Candler \<b.candler@pobox.com\> wrote:
> 
> > The actual method "send" can take a block, and it passes it to the  
> > method being invoked. If it didn't, it would be impossible to pass a  
> > block to a method when invoking it using send.

[Next page](https://rubytalk.org/t/send-with-a-block/62112.md?page=2)
