How to get a reference to a block (when no explicit block parameter is used?)

In a function, I can find out if a block was given using block_given?, but there's no way I can find to get hold of that block if it's there--I can only yield to it, so far as I can tell. I know there has to be a method somewhere to do this, could some kind soul enlighten me?

Many thanks,
Ken

def my_meth param, &block
  p block
end

Regards,
Jan Friedrich

···

Kenneth McDonald <kenneth.m.mcdonald@sbcglobal.net> wrote:

In a function, I can find out if a block was given using block_given?,
but there's no way I can find to get hold of that block if it's there--
I can only yield to it, so far as I can tell. I know there has to be a
method somewhere to do this, could some kind soul enlighten me?

Many thanks,
Ken

Ken, you can use Proc.new

  http://www.ruby-doc.org/core/classes/Proc.html#M001573

Regards,
Pit

Unfortunately, that produces a method that _requires_ a block--I want
one where the block is optional. But thanks for the feedback.

Ken

···

On Nov 26, 2008, at 10:30 AM, Jan Friedrich wrote:

Kenneth McDonald <kenneth.m.mcdonald@sbcglobal.net> wrote:

In a function, I can find out if a block was given using block_given?,
but there's no way I can find to get hold of that block if it's there--
I can only yield to it, so far as I can tell. I know there has to be a
method somewhere to do this, could some kind soul enlighten me?

Many thanks,
Ken

def my_meth param, &block
p block
end

Regards,
Jan Friedrich

The way I understood the question was as follows:

def my_method(arg)
  if block_given?
    yield(arg)
  end
end

How to get a reference to the given block in this situation? This is
especially true if you use define_method, which cannot specify a block param
(at least not in 1.8). Any suggestions?

···

2008/11/26 Jan Friedrich <janfri.rubyforge@gmail.com>

Kenneth McDonald <kenneth.m.mcdonald@sbcglobal.net> wrote:

> In a function, I can find out if a block was given using block_given?,
> but there's no way I can find to get hold of that block if it's there--
> I can only yield to it, so far as I can tell. I know there has to be a
> method somewhere to do this, could some kind soul enlighten me?
>
> Many thanks,
> Ken

def my_meth param, &block
p block
end

Kenneth McDonald wrote:

Unfortunately, that produces a method that _requires_ a block

No, it does not.

HTH,
Sebastian Hungerecker

···

--
NP: Die Ärzte - Mondo Bondage
Jabber: sepp2k@jabber.org
ICQ: 205544826

Unfortunately, that produces a method that _requires_ a block--I want
one where the block is optional. But thanks for the feedback.

Did you try it?

my_meth(5) { puts 'hi' }

#<Proc:0x0006ae28@(irb):10>

my_meth(5)

nil

···

On Nov 26, 10:54 am, Kenneth McDonald <kenneth.m.mcdon...@sbcglobal.net> wrote:

--
-yossef

Whoops, apologies to all. Didn't realize &b arguments were optional.

K

···

On Nov 26, 2008, at 11:00 AM, Yossef Mendelssohn wrote:

On Nov 26, 10:54 am, Kenneth McDonald > <kenneth.m.mcdon...@sbcglobal.net> wrote:

Unfortunately, that produces a method that _requires_ a block--I want
one where the block is optional. But thanks for the feedback.

Did you try it?

my_meth(5) { puts 'hi' }

#<Proc:0x0006ae28@(irb):10>

my_meth(5)

nil

--
-yossef