From: Josh Cheek Date: 2011-03-14T11:30:57+09:00 Subject: Re: send() with a block? --20cf3054acdbe3dc6d049e6819c2 Content-Type: text/plain; charset=ISO-8859-1 On Sat, Mar 12, 2011 at 2:45 PM, 7stud -- 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: > > 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 > > -- > Posted via http://www.ruby-forum.com/. > > 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) [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], #] You can see, it passes on ordinal arguments, args with default values, variable lengthed arguments, and yes, also blocks. --20cf3054acdbe3dc6d049e6819c2--