Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to get a reference to a block (when no explicit block parameteris used?)

Reply
Thread Tools

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

 
 
Kenneth McDonald
Guest
Posts: n/a
 
      11-26-2008
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

 
Reply With Quote
 
 
 
 
Jan Friedrich
Guest
Posts: n/a
 
      11-26-2008
Kenneth McDonald <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Pit Capitain
Guest
Posts: n/a
 
      11-26-2008
Ken, you can use Proc.new

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

Regards,
Pit

 
Reply With Quote
 
Kenneth McDonald
Guest
Posts: n/a
 
      11-26-2008
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 <(E-Mail Removed)> 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
>



 
Reply With Quote
 
James Coglan
Guest
Posts: n/a
 
      11-26-2008
[Note: parts of this message were removed to make it a legal post.]

2008/11/26 Jan Friedrich <(E-Mail Removed)>

> Kenneth McDonald <(E-Mail Removed)> 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




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.. Any suggestions?

 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      11-26-2008
Kenneth McDonald wrote:
> Unfortunately, that produces a method that _requires_ a block


No, it does not.

HTH,
Sebastian Hungerecker
=2D-=20
NP: Die =C4rzte - Mondo Bondage
Jabber: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
Yossef Mendelssohn
Guest
Posts: n/a
 
      11-26-2008
On Nov 26, 10:54=A0am, Kenneth McDonald
<(E-Mail Removed)> 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

 
Reply With Quote
 
Kenneth McDonald
Guest
Posts: n/a
 
      11-27-2008
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
> <(E-Mail Removed)> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
why I need explicit static_cast to const reference Grizlyk C++ 5 02-01-2007 09:16 AM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
What's the difference betwwen explicit instantiaion and explicit specialization? Andy C++ 5 01-30-2005 11:46 PM
Is explicit template qualification required for explicit delete? J.T. Conklin C++ 1 08-11-2004 02:06 AM



Advertisments