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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
47df9cfb356c3ee0523cc3571b169730?d=identicon&s=25 Kenneth McDonald (Guest)
on 2008-11-26 17:29
(Received via mailing list)
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
B32a1911d783e0a95e0b062fd5d0b64b?d=identicon&s=25 Jan Friedrich (janfri)
on 2008-11-26 17:36
(Received via mailing list)
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
50b2daf0e7666574579b9edaf8f2b69a?d=identicon&s=25 Pit Capitain (Guest)
on 2008-11-26 17:43
(Received via mailing list)
Ken, you can use Proc.new

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

Regards,
Pit
47df9cfb356c3ee0523cc3571b169730?d=identicon&s=25 Kenneth McDonald (Guest)
on 2008-11-26 18:00
(Received via mailing list)
Unfortunately, that produces a method that _requires_ a block--I want
one where the block is optional. But thanks for the feedback.

Ken
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2008-11-26 18:03
(Received via mailing list)
2008/11/26 Jan Friedrich <janfri.rubyforge@gmail.com>

> def my_meth param, &block
>  p block
> end



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?
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2008-11-26 18:05
(Received via mailing list)
Kenneth McDonald wrote:
> Unfortunately, that produces a method that _requires_ a block

No, it does not.

HTH,
Sebastian Hungerecker
B1b1d33e0655e841d4fd8467359c58d0?d=identicon&s=25 Yossef Mendelssohn (Guest)
on 2008-11-26 18:06
(Received via mailing list)
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
47df9cfb356c3ee0523cc3571b169730?d=identicon&s=25 Kenneth McDonald (Guest)
on 2008-11-27 04:18
(Received via mailing list)
Whoops, apologies to all. Didn't realize &b arguments were optional.

K
This topic is locked and can not be replied to.