[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

specify a mandatory block parameter

Shea Martin

3/16/2006 8:05:00 PM

I think I know the answer to this already, but...

Is there a what to make a block parameter mandatory?

<code>
def no_block( p_str )
puts "#{p_str}"
end

def block( &p_block )
puts "#{p_block}"
end

no_block #this will cause an exception
block #this doesn't, but is still missing parameter?
</code>

I know, I could just raise my own exception if p_block == nil, just
wondering why the difference? Or am I wrong?

Thanks,

~S
14 Answers

dblack

3/16/2006 8:29:00 PM

0

Jeff Schwab

3/16/2006 8:29:00 PM

0

Shea Martin wrote:
> I think I know the answer to this already, but...
>
> Is there a what to make a block parameter mandatory?
>
> <code>
> def no_block( p_str )
> puts "#{p_str}"
> end
>
> def block( &p_block )
> puts "#{p_block}"
> end
>
> no_block #this will cause an exception
> block #this doesn't, but is still missing parameter?
> </code>
>
> I know, I could just raise my own exception if p_block == nil, just
> wondering why the difference? Or am I wrong?

Do you mean something like this?

def f
throw "D'oh! No block." unless block_given?
puts "OK, got a block."
end

begin
f { }
f
rescue
puts $!
end

dblack

3/16/2006 8:40:00 PM

0

Jeff Schwab

3/16/2006 9:42:00 PM

0

dblack@wobblini.net wrote:

>> def f
>> throw "D'oh! No block." unless block_given?
>
> I think you mean "raise" :-)

I reiterate: D'oh!

Shea Martin

3/17/2006 1:19:00 PM

0

Shea Martin wrote:
> I think I know the answer to this already, but...
>
> Is there a what to make a block parameter mandatory?
>
> <code>
> def no_block( p_str )
> puts "#{p_str}"
> end
>
> def block( &p_block )
> puts "#{p_block}"
> end
>
> no_block #this will cause an exception
> block #this doesn't, but is still missing parameter?
> </code>
>
> I know, I could just raise my own exception if p_block == nil, just
> wondering why the difference? Or am I wrong?
>
> Thanks,
>
> ~S

I think you guys missed part of my post:
"I know, I could just raise my own exception if p_block == nil, just
wondering why the difference?"

Although thank you for reinforcing the concept of raising my own exception.

~S

Jeff Schwab

3/17/2006 2:06:00 PM

0

Shea Martin wrote:
> Shea Martin wrote:
>> I think I know the answer to this already, but...
>>
>> Is there a what to make a block parameter mandatory?
>>
>> <code>
>> def no_block( p_str )
>> puts "#{p_str}"
>> end
>>
>> def block( &p_block )
>> puts "#{p_block}"
>> end
>>
>> no_block #this will cause an exception
>> block #this doesn't, but is still missing parameter?
>> </code>
>>
>> I know, I could just raise my own exception if p_block == nil, just
>> wondering why the difference? Or am I wrong?
>>
>> Thanks,
>>
>> ~S
>
> I think you guys missed part of my post:
> "I know, I could just raise my own exception if p_block == nil, just
> wondering why the difference?"

That's not the same thing. If you use block_given? as David Black and I
suggested, there is no p_block to check for nil.

> Although thank you for reinforcing the concept of raising my own exception.

So... You want an exception to be thrown, but you don't want to throw
an exception? Please show me what I've missed. Are you saying that
specifying the &p_block parameter should make the block mandatory
automatically? Hmmm... I don't know of a way to enforce the presence
of a mandatory block without raising any exceptions.

Shea Martin

3/20/2006 3:21:00 PM

0

Jeffrey Schwab wrote:

> So... You want an exception to be thrown, but you don't want to throw
> an exception? Please show me what I've missed. Are you saying that
> specifying the &p_block parameter should make the block mandatory
> automatically?

I am not saying it *should*, but just curious why specifying a block,
does not make it mandatory (automatically)? Specifying a normal
parameter, makes it mandatory (automatically. It just seems odd that
the two are treated differently. Just my $0.02.

~S

dblack

3/20/2006 3:42:00 PM

0

13

3/21/2006 7:03:00 PM

0

Hi,

Blocks are actually objects of class Proc, so if you need to enforce
presence of block parameter then you can do something like this:

irb(main):001:0> p = Proc.new { puts 'block' }
=> #<Proc:0x02ba5b00@(irb):1>
irb(main):002:0> def method_with_mandatory_block(block)
irb(main):003:1> block.call
irb(main):004:1> end
irb(main):005:0> method_with_mandatory_block p
block

--
Martins

On 3/20/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
>
> On Tue, 21 Mar 2006, Shea Martin wrote:
>
> > Jeffrey Schwab wrote:
> >
> >> So... You want an exception to be thrown, but you don't want to throw an
> >> exception? Please show me what I've missed. Are you saying that
> >> specifying the &p_block parameter should make the block mandatory
> >> automatically?
> >
> > I am not saying it *should*, but just curious why specifying a block, does
> > not make it mandatory (automatically)? Specifying a normal parameter, makes
> > it mandatory (automatically. It just seems odd that the two are treated
> > differently. Just my $0.02.
>
> It's hard for me to answer, because I don't understand why one would
> assume, a priori, that they *should* be treated the same :-) But in
> practice, the code-block facility works out better, I think, the way
> it is. The method can easily branch on existence or non-existence of
> a block, and it's easy to write methods that can work with or without
> a block. I guess I view the &block thing as a special concession to
> the possibility that one might want to handle the block as an object
> (rather than as a syntactic construct), but not the heart of the
> matter.
>
>
> David
>
> --
> David A. Black (dblack@wobblini.net)
> Ruby Power and Light, LLC (http://www.rubypoweran...)
>
> "Ruby for Rails" chapters now available
> from Manning Early Access Program! http://www.manning.com/b...
>
>


Eric Schwartz

3/22/2006 11:39:00 PM

0

13 <one.three@gmail.com> writes:
> Blocks are actually objects of class Proc

Are you sure? I did a lot of reading up on blocks lately, and from
what I learned, blocks are one of the very few things in Ruby that are
not objects. You can, however, automatically convert them to Proc
objects if you need to; maybe that's what you're noticing?

-=Eric